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;
}







0















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?










share|improve this question





























    0















    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?










    share|improve this question

























      0












      0








      0


      1






      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 13:04









      argledhelargledhel

      85213




      85213
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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.






          share|improve this answer































            0














            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>





            share|improve this answer
























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              1














              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.






              share|improve this answer




























                1














                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.






                share|improve this answer


























                  1












                  1








                  1







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 15:01









                  RajNRajN

                  3,67321122




                  3,67321122

























                      0














                      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>





                      share|improve this answer




























                        0














                        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>





                        share|improve this answer


























                          0












                          0








                          0







                          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>





                          share|improve this answer













                          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>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 20:01









                          argledhelargledhel

                          85213




                          85213






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              List item for chat from Array inside array React Native

                              Thiostrepton

                              Caerphilly