带标志枚举的多选组合框
Multiselect Combobox w/ Flags Enum
我希望有人能帮我解决这个问题。我之前问过类似的问题,但当时我没有任何开始。我找到了 SO 问题 link
这与我的问题类似,但有一个问题。组合框不显示 selected 枚举。我在 link 中的示例在我的示例应用程序中工作,但我不知道如何让组合框的文本显示当前 selected 项目。有人对做什么有建议吗?我真的坚持这个。
这是我当前的组合框:
<ComboBox>
<CheckBox Content="SAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SAW}}" />
<CheckBox Content="FCAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.FCAW}}" />
<CheckBox Content="SMAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SMAW}}" />
</ComboBox>
我的转换器是:
public class WeldingProcessFlagValueConverter : IValueConverter
{
private WeldingProcess target;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
WeldingProcess mask = (WeldingProcess)parameter;
this.target = (WeldingProcess)value;
return ((mask & this.target) != 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
this.target ^= (WeldingProcess)parameter;
return this.target;
}
}
所以当我 select 复选框的任意组合时,我的 'CurWeldingProcess' 显示正确的值,但我不知道如何让组合框显示 selected 值( 'CurWeldingProcess')。有什么想法吗?
如果您需要显示所选项目的 "concatenation"(即,如果我检查 SAW 和 SMAW 枚举值,我希望在 ComboBox 文本中看到类似 "SAW, SMAW" 的内容),您可以看看这个Multi Select ComboBox in WPF.
您会发现 MVVM 版本和 "codebehind" 版本。
编辑
好的,您可以去 CodeProject 下载 MultiSelectComboBox dll。将其添加到您的项目中。
然后在你的 XAML 中你可以添加:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:multi="clr-namespace:MultiSelectComboBox;assembly=MultiSelectComboBox"
Title="MainWindow" Height="350" Width="600">
<!-- your xaml -->
<multi:MultiSelectComboBox Margin="4" Name="MultiSelectComboBox" />
<!-- the rest of your xaml -->
</Window>
然后在您的代码隐藏中(我在示例中使用了 TextAlignment
枚举):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Dictionary<string, object> itemsSource = new Dictionary<string, object>();
itemsSource.Add(Convert.ToString(TextAlignment.Center), TextAlignment.Center);
itemsSource.Add(Convert.ToString(TextAlignment.Justify), TextAlignment.Justify);
itemsSource.Add(Convert.ToString(TextAlignment.Left), TextAlignment.Left);
itemsSource.Add(Convert.ToString(TextAlignment.Right), TextAlignment.Right);
MultiSelectComboBox.ItemsSource = itemsSource;
}
}
MultiSelectComboBox 的 SelectedItems
属性 将包含用户选择的值。
这是你需要的吗?
如果您使用的是 MVVM,则可以使用 ViewModel 公开 ItemsSource 和 SelectedItems 字典。
我希望有人能帮我解决这个问题。我之前问过类似的问题,但当时我没有任何开始。我找到了 SO 问题 link
这与我的问题类似,但有一个问题。组合框不显示 selected 枚举。我在 link 中的示例在我的示例应用程序中工作,但我不知道如何让组合框的文本显示当前 selected 项目。有人对做什么有建议吗?我真的坚持这个。
这是我当前的组合框:
<ComboBox>
<CheckBox Content="SAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SAW}}" />
<CheckBox Content="FCAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.FCAW}}" />
<CheckBox Content="SMAW" IsChecked="{Binding Path=CurWeldingProcess, Converter={StaticResource wpFlagValueConverter}, ConverterParameter={x:Static models:WeldingProcess.SMAW}}" />
</ComboBox>
我的转换器是:
public class WeldingProcessFlagValueConverter : IValueConverter
{
private WeldingProcess target;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
WeldingProcess mask = (WeldingProcess)parameter;
this.target = (WeldingProcess)value;
return ((mask & this.target) != 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
this.target ^= (WeldingProcess)parameter;
return this.target;
}
}
所以当我 select 复选框的任意组合时,我的 'CurWeldingProcess' 显示正确的值,但我不知道如何让组合框显示 selected 值( 'CurWeldingProcess')。有什么想法吗?
如果您需要显示所选项目的 "concatenation"(即,如果我检查 SAW 和 SMAW 枚举值,我希望在 ComboBox 文本中看到类似 "SAW, SMAW" 的内容),您可以看看这个Multi Select ComboBox in WPF.
您会发现 MVVM 版本和 "codebehind" 版本。
编辑
好的,您可以去 CodeProject 下载 MultiSelectComboBox dll。将其添加到您的项目中。 然后在你的 XAML 中你可以添加:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:multi="clr-namespace:MultiSelectComboBox;assembly=MultiSelectComboBox"
Title="MainWindow" Height="350" Width="600">
<!-- your xaml -->
<multi:MultiSelectComboBox Margin="4" Name="MultiSelectComboBox" />
<!-- the rest of your xaml -->
</Window>
然后在您的代码隐藏中(我在示例中使用了 TextAlignment
枚举):
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Dictionary<string, object> itemsSource = new Dictionary<string, object>();
itemsSource.Add(Convert.ToString(TextAlignment.Center), TextAlignment.Center);
itemsSource.Add(Convert.ToString(TextAlignment.Justify), TextAlignment.Justify);
itemsSource.Add(Convert.ToString(TextAlignment.Left), TextAlignment.Left);
itemsSource.Add(Convert.ToString(TextAlignment.Right), TextAlignment.Right);
MultiSelectComboBox.ItemsSource = itemsSource;
}
}
MultiSelectComboBox 的 SelectedItems
属性 将包含用户选择的值。
这是你需要的吗?
如果您使用的是 MVVM,则可以使用 ViewModel 公开 ItemsSource 和 SelectedItems 字典。