gpt4 book ai didi

c# - 使用 "combobox with checkbox"在组合框中显示选中的项目

转载 作者:行者123 更新时间:2023-11-30 16:19:37 25 4
gpt4 key购买 nike

我正在构建一个 Windows 应用商店应用程序,由于 UI 问题,我必须在组合框内实现复选框。我遇到了以下问题:我想在“带复选框的组合框”中显示选中的项目。我想做的事 : http://blogs.microsoft.co.il/blogs/justguy/image_2827F1EB.png

     <ComboBox x:Name="cb2"            

DropDownOpened="cb2_DropDownOpened_1"
DropDownClosed="cb2_DropDownClosed_1"
SelectionChanged="cb2_SelectionChanged_1"
Width="310"
ItemsSource="{Binding Members}"
DisplayMemberPath="{Binding Name}"
Height="50" BorderBrush="#FF0A2562"
Tag="{Binding index}"

>


<ComboBox.ItemTemplate>
<DataTemplate>

<CheckBox
Background="Black"
BorderBrush="Black"
Tag="{RelativeSource TemplatedParent}"
Content="{Binding Name}"
IsChecked="{Binding Path=IsSelected,Mode=OneWay}"
Unchecked="CheckBox_Unchecked_1"
Click="CheckBox_Click"
/>
</DataTemplate>

</ComboBox.ItemTemplate>

</ComboBox>

代码隐藏:

private void CheckBox_Click(object sender, RoutedEventArgs e)
{

CheckBox chk = (CheckBox)sender;


string k =(string) chk.Content;

chkstr.Add(k);


}
private void CheckBox_Unchecked_1(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
string k = (string)chk.Content;

chkstr.Remove(k);
// vl.selectedmembers.Remove(key);
}

最佳答案

在微软定义的ComboBox Standard Template中,在ContentPresenter中添加一个TextBlock。将 TextBlock 的 Text 属性与字符串绑定(bind)。现在您可以通过将项目显式转换为字符串来将选中的项目添加到字符串中。@Patrick: ItemTemplate 和 ItemContainerStyle 都可以。

关于c# - 使用 "combobox with checkbox"在组合框中显示选中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15088137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com