简体   繁体   English

隐藏复选框,但显示其内容

[英]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.我想在关闭编辑模式时隐藏列表框中的复选框(因此它应该绑定到 EditModeSelected),但文本应该保持可见。

In order to do so You can keep two TextBlocks.为此,您可以保留两个 TextBlock。 In edit mode visible CheckBox and hide TextBlock and in reader mode vice versa.在编辑模式下可见 CheckBox 并隐藏 TextBlock,在阅读器模式下反之亦然。 I hope this may help.我希望这可能会有所帮助。 As DataTemplate can have only one child here's the fix由于 DataTemplate 只能有一个孩子,这是修复

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.现在在 .cs 文件中根据要求分配日期模板。

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:我想到了 3 种可能的解决方案 - 其中两个或多或少是“黑客”,一个或多或少是干净的解决方案:

  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.这是从我正在处理的项目中提取的 Xaml 唯一解决方案。 In this case "ShowCheck" is a field in the current binding context saying whether or not to show the check.在这种情况下,“ShowCheck”是当前绑定上下文中的一个字段,表示是否显示检查。

<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如果您将更复杂的对象放入复选框中,您可能需要将 ContentTemplate、ContentTemplateSelector 和相关字段移动到用于替换复选框的 ContentControl 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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