【问题标题】:How To Reset the isChecked property of checkbox in datatemplate of a listbox如何在列表框的数据模板中重置复选框的 isChecked 属性
【发布时间】:2011-02-26 18:57:13
【问题描述】:
<StackPanel Grid.Column="0" >
        <ToggleButton Name="buttonEditListBoxItem" 
                      Content="Edit" 
                      IsChecked="False"
                      Click="buttonEditListBoxItem_Click"></ToggleButton>
        <ListBox  Name="ListBoxTriggers" 
             SelectedValuePath="TriggerId" 
             IsSynchronizedWithCurrentItem="True"
             SelectionChanged="Triggers_SelectionChanged"
             ScrollViewer.HorizontalScrollBarVisibility="Auto" 
             HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>  <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="AUTO"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <CheckBox Visibility="{Binding ElementName=buttonEditListBoxItem, Path=IsChecked, Converter={StaticResource visibilityConverter}}" Grid.Column="0" VerticalAlignment="Center" x:Name="checkBoxTriggers" ></CheckBox>
                        <Button Grid.Column="1" Style="{StaticResource GlassButton}" 
                        Uid="{Binding Path=TriggerId}" 
                        Margin="5"
                       x:Name="ButtonTrigger"
                       GotFocus="ButtonTrigger_GotFocus"
                      >
                            <Button.Content>
                                <TextBlock Foreground="White" TextAlignment="Justify"
                                   TextWrapping="Wrap" Margin="6"   Text="{Binding Path=Name}"/>
                            </Button.Content>
                        </Button>
                    </Grid>
                </DataTemplate> </ListBox.ItemTemplate>
    </ListBox>

我有一个列表框的数据模板,它有一个按钮和一个复选框。列表框外有一个 ToggleButton,它决定了复选框的可见性。 问题是,如果我单击了 ToggleButton 并且复选框可见,并且当我再次单击 ToggleButton 时检查了一些复选框,则复选框被隐藏,但我希望重置复选框;当它们再次可见时,我不想点击任何复选框。

【问题讨论】:

    标签: wpf listbox datatemplate checkbox


    【解决方案1】:

    您可以为 CheckBox 的 IsVisibleChanged 事件添加处理程序:

    IsVisibleChanged="checkBoxTriggers_IsVisibleChanged"
    

    并在句柄中清除 IsChecked 标志:

    private void checkBoxTriggers_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
      CheckBox cb = sender as CheckBox;
    
      if (!cb.IsVisible) 
        cb.IsChecked = false;
    }
    

    使用类似这样的触发器可能会达到相同的效果:

    <Trigger Property="IsVisible" Value="false">
      <Setter Property="IsChecked" Value="false" />
    </Trigger>
    

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 2015-05-19
      • 2014-04-16
      • 2012-04-12
      • 1970-01-01
      • 2013-02-11
      • 2015-05-02
      • 1970-01-01
      • 2020-11-16
      相关资源
      最近更新 更多