将 RadioButton 绑定到 Enum

时间:2021-07-12 19:24:33

标签: c# xaml enums

所以我有一个枚举类

namespace DogsNSuch.Data
    {
        /// <summary>
        /// Enum for the type of buns
        /// </summary>
        public enum Bun
        {
            White,
            SesameSeed,
            Hoagie,
            PizzaDough,
            CornBreading
        }
    }

我想将它绑定到我的 xaml 单选按钮,每种类型的 bun 应该有 5 个单选按钮

<UserControl x:Class="PointOfSale.DogCustomization"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:PointOfSale" xmlns:data="clr-namespace:DogsNSuch.Data;assembly=Data" 
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="500">
<UserControl.Resources>
    <ObjectDataProvider x:Key="fruits" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Status"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <RadioButton Content="{TemplateBinding ContentPresenter.Content}" IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

我有一个狗定制 xaml cs

    namespace PointOfSale
{
    /// <summary>
    /// Interaction logic for DogCustomization.xaml
    /// </summary>
    public partial class DogCustomization : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public DogCustomization()
        {
            InitializeComponent();
        }

        private Bun bun;

        public Bun Bun
        {
            get { return bun; }
            set
            {
                bun = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Bun"));
            }
        }

在我的主窗口中,我设置了数据上下文。网上的每个人都说使用转换器,但是当我尝试使用它时,它找不到我的 bun 枚举。我的 bun 在项目 DogsNuch.data 中,而我的 xaml 在销售点。有没有办法将单选按钮绑定到枚举类?如果您需要更多信息,请告诉我。

0 个答案:

没有答案
相关问题