【问题标题】:[.net 4.5]How to Binding enum to RadioButton ?Why x:static not support enum?[.net 4.5]如何将枚举绑定到 RadioButton ?为什么 x:static 不支持枚举?
【发布时间】:2017-03-24 09:07:16
【问题描述】:

当我需要将枚举绑定到 RadioButton 时,请参考 How to bind RadioButtons to an enum? 问题的更好答案。

枚举代码:

public enum SelectionMode
{
    FixedMode = 0,
    FreeMode = 1
}

/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

xml代码:

<RadioButton Content="FreeMode" IsChecked="{Binding ChannelSelectionMode, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static local:SelectionMode.FreeMode}}" ></RadioButton>

我有这个例外:

xaml 异常:namaspace 未找到 SelectionMode、枚举 命名空间中的SelectionMode,x:Type Local:SelectionMode可以是 工作。

我试过这样:

  1. 将枚举 SelectionMode 移动到 Viewmodels 的命名空间。
  2. 将枚举 SelectionMode 移动到一个 Viewmodel 类中。

但都不起作用。 请给我一些建议,我疯了。

【问题讨论】:

  • 你的转换器代码在哪里?

标签: enums binding


【解决方案1】:

您分享的帖子有转换器代码,或者您也可以在此帖子中找到。 Binding radio button to enum property

您还需要添加另一个类似这样的单选按钮

<RadioButton Content="FixedMode" IsChecked="{Binding ChannelSelectionMode, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static local:SelectionMode.FixedMode}}" ></RadioButton>
<RadioButton Content="FreeMode" IsChecked="{Binding ChannelSelectionMode, Converter={StaticResource enumToBooleanConverter}, ConverterParameter={x:Static local:SelectionMode.FreeMode}}" ></RadioButton>

【讨论】:

  • 感谢您的回答,现在我再次重建。它有效,嗯....大多数时候重新启动VS将是一个好方法。 [笑哭]
【解决方案2】:

如果有问题,别着急,重启VS,离开一会儿再回来

【讨论】:

    猜你喜欢
    • 2015-10-17
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多