gpt4 book ai didi

wpf - 寻找带有复选框的 WPF ComboBox

转载 作者:行者123 更新时间:2023-12-03 07:54:08 26 4
gpt4 key购买 nike

我的谷歌技能让我失望。任何人都听说过类似 WPF 的控件。我试图让它看起来像这样(winforms screenshot)。

最佳答案

有我的组合框。我使用 Martin Harris 代码和来自此链接的代码 Can a WPF ComboBox display alternative text when its selection is null?

<ComboBox Name="cbObjects" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="2,2,6,0" SelectionChanged="OnCbObjectsSelectionChanged" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" Width="20" VerticalAlignment="Center" Checked="OnCbObjectCheckBoxChecked" Unchecked="OnCbObjectCheckBoxChecked" />
<TextBlock Text="{Binding ObjectData}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock IsHitTestVisible="False" Name="tbObjects" Text="Выберите объекты..." Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="6,2,6,0" />

数据源小类:
public class SelectableObject <T> {
public bool IsSelected { get; set; }
public T ObjectData { get; set; }

public SelectableObject(T objectData) {
ObjectData = objectData;
}

public SelectableObject(T objectData, bool isSelected) {
IsSelected = isSelected;
ObjectData = objectData;
}
}

并且有两个处理程序 - 一个用于处理单击的 CheckBox,一个用于为 ComboBox 形成文本。
private void OnCbObjectCheckBoxChecked(object sender, RoutedEventArgs e) {
StringBuilder sb = new StringBuilder();
foreach (SelectableObject<tblObject> cbObject in cbObjects.Items)
{
if (cbObject.IsSelected)
sb.AppendFormat("{0}, ", cbObject.ObjectData.Description);
}
tbObjects.Text = sb.ToString().Trim().TrimEnd(',');
}

private void OnCbObjectsSelectionChanged(object sender, SelectionChangedEventArgs e) {
ComboBox comboBox = (ComboBox)sender;
comboBox.SelectedItem = null;
}

对于 ComboBox.ItemsSource 我使用
ObservableCollection<SelectableObject<tblObject>> 

其中 tblObject 是我的对象的类型,我想在 ComboBox 中显示它的列表。

我希望这段代码对某人有用!

关于wpf - 寻找带有复选框的 WPF ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/859227/

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