简体   繁体   中英

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.

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

3 possible solutions come in my mind - two of them more or less "hacks" and one more or less clean solution:

  1. A checkbox and textblock for every item - you can get problems with margins etc
  2. A checkbox with no content (which is only visible when in edit mode), and a textblock which is always visible
  3. Take the default controltemplate for checkbox ( Default ControlTemplate for CheckBox ) and bind the visibility of the checkbox

Here is a Xaml only solution pulled from a project I am working on. In this case "ShowCheck" is a field in the current binding context saying whether or not to show the check.

<CheckBox Content="{Binding Name}">
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ShowCheck}" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="CheckBox">
                                <ContentControl Content="{TemplateBinding Content}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

Basically if the checkbox should be invisible, then I use a style and a trigger to change the checkbox's template to something without the checkbox. My implementation the content is just a string, so this works. If you were putting more complicated objects into the checkbox, you might need to shuttle the ContentTemplate, ContentTemplateSelector, and related fields into the ContentControl that is used to replace the checkbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM