Problem: WPF Checkbox inside Listbox is disabled but still clickable
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a Style for a listbox, using checkboxes. I want to bind the isEnabled property of each checkbox to a property (ItemEnabled) of each item. This is my code:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}">
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
So far, the checkboxes which have the property ItemEnabled set to false, are grayed out. However, still clickable and checkable/uncheckable.
Any ideas?
wpf checkbox
add a comment |
I have a Style for a listbox, using checkboxes. I want to bind the isEnabled property of each checkbox to a property (ItemEnabled) of each item. This is my code:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}">
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
So far, the checkboxes which have the property ItemEnabled set to false, are grayed out. However, still clickable and checkable/uncheckable.
Any ideas?
wpf checkbox
add a comment |
I have a Style for a listbox, using checkboxes. I want to bind the isEnabled property of each checkbox to a property (ItemEnabled) of each item. This is my code:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}">
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
So far, the checkboxes which have the property ItemEnabled set to false, are grayed out. However, still clickable and checkable/uncheckable.
Any ideas?
wpf checkbox
I have a Style for a listbox, using checkboxes. I want to bind the isEnabled property of each checkbox to a property (ItemEnabled) of each item. This is my code:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}">
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
So far, the checkboxes which have the property ItemEnabled set to false, are grayed out. However, still clickable and checkable/uncheckable.
Any ideas?
wpf checkbox
wpf checkbox
asked Nov 16 '18 at 13:04
argledhelargledhel
85213
85213
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If I understand your question correctly, you want to control the checkbox 'IsEnabled' controlled via model, 'IsChecked' controlled by selection of listview selection.
You can do by simply creating DataTemplate for your model
<DataTemplate DataType="{x:Type local:TestModel}">
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ItemEnabled}"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
TestModel:
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public bool ItemEnabled { get; set; }
}
Remove the 'ItemContainerStyle' from your code.
Hope this helps.
add a comment |
Thanks, RajN.
I managed to solve it based on your comment.
Here is the new XAML:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ListBoxItem} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}"
Content="{Binding Path=ItemName, Mode=OneWay}">
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338501%2fproblem-wpf-checkbox-inside-listbox-is-disabled-but-still-clickable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand your question correctly, you want to control the checkbox 'IsEnabled' controlled via model, 'IsChecked' controlled by selection of listview selection.
You can do by simply creating DataTemplate for your model
<DataTemplate DataType="{x:Type local:TestModel}">
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ItemEnabled}"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
TestModel:
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public bool ItemEnabled { get; set; }
}
Remove the 'ItemContainerStyle' from your code.
Hope this helps.
add a comment |
If I understand your question correctly, you want to control the checkbox 'IsEnabled' controlled via model, 'IsChecked' controlled by selection of listview selection.
You can do by simply creating DataTemplate for your model
<DataTemplate DataType="{x:Type local:TestModel}">
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ItemEnabled}"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
TestModel:
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public bool ItemEnabled { get; set; }
}
Remove the 'ItemContainerStyle' from your code.
Hope this helps.
add a comment |
If I understand your question correctly, you want to control the checkbox 'IsEnabled' controlled via model, 'IsChecked' controlled by selection of listview selection.
You can do by simply creating DataTemplate for your model
<DataTemplate DataType="{x:Type local:TestModel}">
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ItemEnabled}"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
TestModel:
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public bool ItemEnabled { get; set; }
}
Remove the 'ItemContainerStyle' from your code.
Hope this helps.
If I understand your question correctly, you want to control the checkbox 'IsEnabled' controlled via model, 'IsChecked' controlled by selection of listview selection.
You can do by simply creating DataTemplate for your model
<DataTemplate DataType="{x:Type local:TestModel}">
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ItemEnabled}"
IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"
></CheckBox>
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
TestModel:
public class Emp
{
public int ID { get; set; }
public string Name { get; set; }
public bool ItemEnabled { get; set; }
}
Remove the 'ItemContainerStyle' from your code.
Hope this helps.
answered Nov 16 '18 at 15:01
RajNRajN
3,67321122
3,67321122
add a comment |
add a comment |
Thanks, RajN.
I managed to solve it based on your comment.
Here is the new XAML:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ListBoxItem} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}"
Content="{Binding Path=ItemName, Mode=OneWay}">
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
add a comment |
Thanks, RajN.
I managed to solve it based on your comment.
Here is the new XAML:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ListBoxItem} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}"
Content="{Binding Path=ItemName, Mode=OneWay}">
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
add a comment |
Thanks, RajN.
I managed to solve it based on your comment.
Here is the new XAML:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ListBoxItem} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}"
Content="{Binding Path=ItemName, Mode=OneWay}">
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
Thanks, RajN.
I managed to solve it based on your comment.
Here is the new XAML:
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter></ContentPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Focusable="False"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=ListBoxItem} }"
IsEnabled="{Binding Path=ItemEnabled, Mode=OneWay}"
Content="{Binding Path=ItemName, Mode=OneWay}">
</CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
answered Nov 16 '18 at 20:01
argledhelargledhel
85213
85213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338501%2fproblem-wpf-checkbox-inside-listbox-is-disabled-but-still-clickable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown