Multislect Combobox w / Flags枚举 [英] Multiselect Combobox w/ Flags Enum

查看:102
本文介绍了Multislect Combobox w / Flags枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我。我以前问了一个类似的问题,但我当时没有开始任何事情。我发现了这个问题链接



这与我的问题相似,但有一个问题。组合框不显示所选的枚举。我在示例应用程序中的链接中进行示例,但是我不知道如何获取组合框的文本来显示当前选定的项目。有人有什么建议吗?我真的坚持在这一个。



这是我现在的组合框:

 <组合框> 
< CheckBox Content =SAWIsChecked ={Binding Path = CurWeldingProcess,Converter = {StaticResource wpFlagValueConverter},ConverterParameter = {x:静态模型:WeldingProcess.SAW}}/>
< CheckBox Content =FCAWIsChecked ={Binding Path = CurWeldingProcess,Converter = {StaticResource wpFlagValueConverter},ConverterParameter = {x:Static models:WeldingProcess.FCAW}}/>
< CheckBox Content =SMAWIsChecked ={Binding Path = CurWeldingProcess,Converter = {StaticResource wpFlagValueConverter},ConverterParameter = {x:静态模型:WeldingProcess.SMAW}}/>
< / ComboBox>

我的转换器是:

  public class WeldingProcessFlagValueConverter:IValueConverter 
{
private WeldingProcess target;

public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
WeldingProcess mask =(WeldingProcess)参数;
this.target =(WeldingProcess)值;
return((mask& this.target)!= 0);
}

public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
{
this.target ^ =(WeldingProcess)参数;
return this.target;
}
}

所以我的'CurWeldingProcess'显示正确的值我选择复选框的任何组合,但我不知道如何让组合框显示选定的值('CurWeldingProcess')。任何想法?

解决方案

如果您需要显示所选项目的连接(即如果我检查SAW和SMAW枚举值得注意的是,我想在ComboBox文本中看到SAW,SMAW),你可以看看这个多选择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 =MainWindowHeight =350Width =600>
<! - 你的xaml - >
< multi:MultiSelectComboBox Margin =4Name =MultiSelectComboBox/>
<! - 其余的xaml - >
< / Window>

然后在你的代码隐藏(我用于我的示例 TextAlignment 枚举):

  public partial class MainWindow:Window 
{
public MainWindow ()
{
InitializeComponent();

字典< 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;
}
}

SelectedItems 属性将包含用户选择的值。
这是你需要的吗?
如果您使用MVVM,您可以使用ViewModel公开ItemsSource和SelectedItems字典。


I am hoping someone can help me out with this. I have asked a similar question before but I didn't have anything started on this at the time. I have found the SO question link

that is similar to my problem but it has one issue. The combobox doesn't show the selected enums. I do the example in the link working in my sample app but I have no clue how to get the Combobox's text to show the currently selected item(s). Anyone have a suggestion on what to do? I am real stuck on this one.

Here is my current combobox:

<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>

My Converter is:

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;
    }
}

So my 'CurWeldingProcess' is showing the correct value when I select any combination of the checkboxes, but I dont know how to get the combobox to show the selected values ('CurWeldingProcess'). Any ideas?

解决方案

If you need to show a "concatenation" of the selected items (i.e. if I check SAW and SMAW enum values, I would like to see in the ComboBox Text something like "SAW, SMAW"), you can take a look to this Multi Select ComboBox in WPF.

You will find both a MVVM version and "codebehind" one.

EDIT

Ok, you can go the CodeProject and download the MultiSelectComboBox dll. Add it in your project. Then in your XAML you can add:

<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>

Then in your code-behind (I used for my sample the TextAlignment enum):

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;
    }
}

The SelectedItems property of the MultiSelectComboBox will contain the values that the user selected. Is it what you needed? If you are using MVVM you can expose the ItemsSource and the SelectedItems dictionaries with your ViewModel.

这篇关于Multislect Combobox w / Flags枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆