相关疑难解决方法(0)

将枚举属性数据绑定到WPF中的ComboBox

作为示例,请使用以下代码:

public enum ExampleEnum { FooBar, BarFoo }

public class ExampleClass : INotifyPropertyChanged
{
    private ExampleEnum example;

    public ExampleEnum ExampleProperty 
    { get { return example; } { /* set and notify */; } }
}
Run Code Online (Sandbox Code Playgroud)

我想要将属性ExampleProperty数据绑定到ComboBox,以便它显示选项"FooBar"和"BarFoo"并在TwoWay模式下工作.最理想的是我希望我的ComboBox定义看起来像这样:

<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />
Run Code Online (Sandbox Code Playgroud)

目前我在我的Window中安装了ComboBox.SelectionChanged和ExampleClass.PropertyChanged事件的处理程序,我手动执行绑定.

是否有更好或某种规范的方式?您通常会使用转换器吗?如何使用正确的值填充ComboBox?我现在甚至不想开始使用i18n.

编辑

所以回答了一个问题:如何使用正确的值填充ComboBox.

通过静态Enum.GetValues方法中的ObjectDataProvider将Enum值作为字符串列表检索:

<Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
        ObjectType="{x:Type sys:Enum}"
        x:Key="ExampleEnumValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="ExampleEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

这个我可以用作我的ComboBox的ItemsSource:

<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
Run Code Online (Sandbox Code Playgroud)

.net wpf

246
推荐指数
9
解决办法
17万
查看次数

WPF数据绑定:如何使用XAML将枚举数据绑定到组合框?

我有一节课:

public class AccountDetail
{
    public DetailScope Scope
    {
        get { return scope; }
        set { scope = value; }
    }

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }

    private DetailScope scope;
    private string value;

    public AccountDetail(DetailScope scope, string value)
    {
        this.scope = scope;
        this.value = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

和一个枚举:

public enum DetailScope
{
    Private, 
    Business, 
    OtherDetail
}
Run Code Online (Sandbox Code Playgroud)

最后,我有一个.xaml文件:

<Window x:Class="Gui.Wpf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" 
    SizeToContent="WidthAndHeight">

    <Grid>
        <ComboBox 
            Name="ScopeComboBox" 
            Width="120" 
            Height="23" 
            Margin="12" /> …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf xaml combobox

24
推荐指数
2
解决办法
4万
查看次数

如何在c#中本地化属性描述?

我正在开设一个将被其他国家的一些人使用的课程.我必须本地化每条消息,警告ec,以便他们能理解我们的意思.在许多情况下,我实现了我的目标.但是像描述这些属性属性在屁股中是如此痛苦.

这就是我现在所拥有的:

[Category("Editable Values"), Description("Sets the minimum select...")]
    public Ampere Simin
    {
        get
        {...}
        set
        {...}
    }
Run Code Online (Sandbox Code Playgroud)

[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
        public Ampere Simin
        {
            get
            {...}
            set
            {...}
        }
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的.但是不可能以这种方式使用Localisations.关于我可以使用的东西的任何建议而不是它?

c# localization properties

16
推荐指数
2
解决办法
1万
查看次数

将UWP ComboBox ItemsSource绑定到Enum

可以使用ObjectDataProviderWPF应用程序将枚举的字符串值绑定到ComboBox的ItemsSource,如此问题所示.

但是,在UWP应用程序中使用类似的代码段时,ff.显示错误消息:

"Windows Universal项目不支持ObjectDataProvider."

在UWP中有一个简单的替代方法吗?

c# enums combobox uwp uwp-xaml

9
推荐指数
1
解决办法
4780
查看次数

带有绑定参数的MarkupExtension

我正在开发一个Custom MarkupExtension,我需要一个来自XAML的非字符串参数来构造新对象.是否可以在datacontext范围内的字段上使用非字符串参数绑定?

换句话说,我该怎么办呢?

<ListBox ItemsSource="{Binding Source={local:MyMarkupExtension {x:Type Button},IncludeMethods={Binding Source=CustomerObject.IsProblematic}}}" />
Run Code Online (Sandbox Code Playgroud)

其中IncludeMethods=CustomerObject.IsProblematic给我这个错误:绑定无法在类型"TypeDescriptorExtension"的"IncludeMethods"属性设置.'绑定'只能在DependencyObject的DependencyProperty上设置.

谁能帮我?

谢谢

wpf binding

7
推荐指数
1
解决办法
1万
查看次数

获取Enum <T>值描述

我有我的enumHelper类包含这些:

public static IList<T> GetValues()
{
  IList<T> list = new List<T>();
  foreach (object value in Enum.GetValues(typeof(T)))
  {
    list.Add((T)value);
  }
  return list;
}
Run Code Online (Sandbox Code Playgroud)

public static string Description(Enum value)
{
  Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute));
  if (DescAttribute == null)
    return value.ToString();
  else
    return ((DescriptionAttribute)DescAttribute).Description;
}
Run Code Online (Sandbox Code Playgroud)

我的枚举是这样的:

public enum OutputType
{
    File,
    [Description("Data Table")]
    DataTable
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.所有以前的工作都很好.现在我想添加一个新的帮助器来返回BindingList>,所以我可以将任何枚举链接到任何组合使用

BindingList<KeyValuePair<OutputType, string>> list = Enum<OutputType>.GetBindableList();
cbo.datasource=list;
cbo.DisplayMember="Value";
cbo.ValueMember="Key";
Run Code Online (Sandbox Code Playgroud)

为此,我补充说:

public static BindingList<KeyValuePair<T, string>> GetBindingList()
{
    BindingList<KeyValuePair<T, string>> list = new BindingList<KeyValuePair<T, string>>();
    foreach (T value …
Run Code Online (Sandbox Code Playgroud)

c# generics enums bindinglist

5
推荐指数
2
解决办法
9643
查看次数