【问题标题】:Create a ComboBox containing the values of a checked CheckBoxs创建一个包含选中 CheckBoxes 值的 ComboBox
【发布时间】:2019-10-09 19:56:36
【问题描述】:

我正在创建一个 CheckBoxs 和一个 ComboBox 列表,其中包含 WPF MVVM 应用程序中选中复选框的列表。我不知道如何从复选框中绑定组合框选中值的文本。

这是我尝试过的:

<ComboBox ItemsSource="{Binding Systems}" Grid.Row="4" Grid.Column="1" 
    IsEditable="True" IsReadOnly="True" Text="{}">
     <ComboBox.ItemTemplate>
       <DataTemplate>
        <CheckBox Content="{Binding TemplateName}" IsChecked="{Binding 
          IsSystemChecked, UpdateSourceTrigger=PropertyChanged}"/>
       </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

【问题讨论】:

  • 能否请您重新表述一下您要执行的操作?
  • 我的问题是用户何时检查复选框项目应如何在组合框中显示
  • 所以你想要一个复选框列表和包含选中复选框列表的组合框?
  • 是的,我想要那个

标签: c# wpf xaml mvvm combobox


【解决方案1】:

这应该可以解决问题:

<StackPanel>
    <ListView ItemsSource="{Binding Systems}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding TemplateName}" IsChecked="{Binding 
                    IsSystemChecked, UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <ComboBox ItemsSource="{Binding Systems}" DisplayMemberPath="TemplateName" >
        <ComboBox.Style>
            <Style TargetType="ComboBox">
                <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsSystemChecked}" Value="False">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.Style>
    </ComboBox>
</StackPanel>

这里有:

  • ListView 包含 Systems 中每个项目的复选框,绑定到 IsSystemChecked 属性
  • ComboBox 包含 Systems 中的所有项目,但是如果 IsSystemChecked 属性为 false,则 Visibility 设置为 Collapsed,因此不会显示

如果您有任何问题,请告诉我!希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2021-10-07
    • 2018-09-28
    相关资源
    最近更新 更多