Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide checkbox, but show its content

Is it possible to hide a checkbox, but leave its content visible?

<ListBox  
         ItemsSource ="{Binding MyItemCollection}"     
         SelectionMode="Single" 
         Width="300"
         Height="320">
      <ListBox.ItemTemplate>
          <DataTemplate>
             <CheckBox IsChecked="{Binding IsChecked}">
                   <CheckBox.Content>
                       <TextBlock Text="{Binding Item.Code}"/>
                   </CheckBox.Content>
             </CheckBox>
          </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>
<StackPanel>
   <CheckBox Content="Edit Mode" 
             IsChecked="{Binding Path=EditModeSelected, Mode=TwoWay}">
   </CheckBox>
</StackPanel>

I would like to hide the checkboxes in the list box when I turn Edit Mode off (so it should be binded to EditModeSelected), but the text should be left visible.

like image 672
Dzhara Avatar asked Oct 14 '25 03:10

Dzhara


1 Answers

In order to do so You can keep two TextBlocks. In edit mode visible CheckBox and hide TextBlock and in reader mode vice versa. I hope this may help. As DataTemplate can have only one child here's the fix

Create a Window Resource like below. Two Data Templates were created one for edit mode and another for Reader Mode.

<Window.Resources>
    <DataTemplate x:Key="EditModeTemplate">
        <CheckBox IsChecked="{Binding IsChecked}">
            <CheckBox.Content>
                <TextBlock Text="{Binding Item.Code}"/>
            </CheckBox.Content>
        </CheckBox>
    </DataTemplate>
    <DataTemplate x:Key="ReaderModeTemplate">
        <TextBlock Text="{Binding Item.Code}"/>
    </DataTemplate>
</Window.Resources>

Now in .cs file assign the Date Template as per requirements.

if (EditMode)
{
    DemoCollection.ItemTemplate = this.Resources["EditModeTemplate"] as DataTemplate;
}
else
{
    DemoCollection.ItemTemplate = this.Resources["ReaderModeTemplate"] as DataTemplate;
}
like image 194
Mukesh Kumar Avatar answered Oct 17 '25 13:10

Mukesh Kumar