【问题标题】:Bind Enum to TabControl Tab Headers and Selected Tab将枚举绑定到 TabControl 选项卡标题和选定选项卡
【发布时间】:2018-08-27 12:29:40
【问题描述】:

我有一个要绑定到选项卡控件的枚举。基本上我希望枚举成员名称(描述)填充选项卡标题和选定的选项卡以设置枚举值。

我想要做的是选择选项卡,控制枚举的值。因此,当我选择选项卡“DC Hipot”时,它将枚举值设置为 DCW,当我选择“电阻”​​选项卡时,它将枚举值设置为 LOWOHM。然后我希望相反,如果枚举值发生变化,则选择相应的选项卡。因此,例如,如果枚举值更改为 LOWOHM,则所选选项卡将更改为“阻力”。

我已阅读此线程Binding TabControl to an enum,但它没有提供完整的解决方案。我看不出这与 Tab Control 有什么关系。

这是一个带有单选按钮的示例,以及我想要对 Tabs 和枚举执行的操作的 int。 Simple WPF RadioButton Binding?

具体的枚举是 MV_Main.SelectedTestStepForUI.vTestType。

这是我的 MainWindows.xaml

<Window x:Class="Hipot_Sequence_Editor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Hipot_Sequence_Editor"
        xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:local1="clr-namespace:Hipot_Sequence_Editor.UI_Converters"
        mc:Ignorable="d"
        Title="MainWindow" Height="677.538" Width="896.456">
    <Window.DataContext>
        <local:MV_Main></local:MV_Main>
    </Window.DataContext>
    <Window.Resources>
        <local1:DoubleStringConverter  x:Key="DoubleStringConverter" />
    </Window.Resources>
    <WrapPanel>
        <TabControl>
            <TabItem Header="Resistance">
                <WrapPanel>
                    <WrapPanel>
                        <Label Content="Resistance"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="0.000mO" Text="{Binding SelectedTestStepForUI.MaxResistance, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                    <WrapPanel>
                        <Label Content="Dwell Time"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="0000mS" Text="{Binding SelectedTestStepForUI.TestTimeResistance, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                </WrapPanel>
            </TabItem>
            <TabItem Header="DC Hipot">
                <WrapPanel>
                    <WrapPanel>
                        <Label Content="Voltage"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="0.000kV" Text="{Binding SelectedTestStepForUI.Voltage, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                    <WrapPanel>
                        <Label Content="Ramp Time"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="0000mS" Text="{Binding SelectedTestStepForUI.RampTime, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                    <WrapPanel>
                        <Label Content="Dwell Time"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="0000mS" Text="{Binding SelectedTestStepForUI.DwellTime, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                    <WrapPanel>
                        <Label Content="Max Leakage Current"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="00.000m\A" Text="{Binding SelectedTestStepForUI.MaxLeakageLimit, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                    <WrapPanel>
                        <Label Content="Max  Breakdown Current"></Label>
                        <xctk:MaskedTextBox Width="50" Mask="00.000m\A" Text="{Binding SelectedTestStepForUI.BreakDownLimit, Converter={StaticResource DoubleStringConverter}}"></xctk:MaskedTextBox>
                    </WrapPanel>
                </WrapPanel>
            </TabItem>
        </TabControl>
    </WrapPanel>
</Window>

这是主视图模型,MV_Main 及其依赖项

[AddINotifyPropertyChangedInterface]  //** Fodo
class MV_Main
{

    public MV_Test SelectedTestStepForUI { get; set; }

    public IEnumerable<TestType> TestTypes
    {
        get
        {
            return Enum.GetValues(typeof(TestType))
                .Cast<TestType>();
        }
    }


    public MV_Main()
    {          
        SelectedTestStepForUI = new MV_Test();
    }

}

public enum TestType
{
    [Description("AC Hipot")]
    ACW,
    [Description("DC Hipot")]
    DCW,
    [Description("Resistance")]
    LOWOHM
}

[AddINotifyPropertyChangedInterface] //** Fodo
public class MV_Test
{
    public TestType vTestType { get; set; }
    //** DCW Items
    public double RampTime { get; set; }
    public double DwellTime { get; set; }
    public double Voltage { get; set; }
    public double MaxLeakageLimit { get; set; }
    public double MinLeakageLimit { get; set; }
    public double BreakDownLimit { get; set; }
    public double ArcDetectionTimeLimit { get; set; }
    public double ArcDetectionCurrentLimit { get; set; }

    //** LOWOHM Items
    public double TestTimeResistance { get; set; }
    public double MinResistance { get; set; }
    public double MaxResistance { get; set; }
}

【问题讨论】:

  • 一个TabItem的内容和枚举值有什么关系?
  • 如果您查看我的代码底部的 MV_Test 类,您会发现每种测试类型都有不同的参数。因此,对于 DCW,我将通过 ArcDetectionCurrentLimit 显示 RampTime,对于电阻,我将通过 MaxResistance 显示 TestTimeResistance。我最初在它自己的类中进行了每个测试,但由于硬件的串行接口语法,我将其更改为一个类。
  • 那么一个MV_Test的实例对应于视图中的一个TabItem?
  • 不,整个 TabControl 将松散地对应于 MV_Test 的 1 个实例。还没有达到拥有多个 MV_Test 实例的程度。我想要做的是选择选项卡,控制枚举的值。因此,当我选择选项卡“DC Hipot”时,它将枚举值设置为 DCW,当我选择“电阻”​​选项卡时,它将枚举值设置为 LOWOHM。然后我希望相反,如果枚举值发生变化,则选择相应的选项卡。因此,例如,如果枚举值更改为 LOWOHM,则所选选项卡将更改为“阻力”。

标签: c# wpf data-binding enums


【解决方案1】:

我终于找到了使用 IValueConverter 的解决方案

这是我的转换器代码

public class EnumSelectedConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || parameter == null) return false;
        TestType vmType = (TestType) value;
        TestType viewType = (TestType) parameter;

        return vmType == viewType;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null || parameter == null) return false;

        bool isSelected = (bool) value;
        if (!isSelected) return false;

        TestType vTestType = (TestType) parameter;

        return vTestType;
    }
}

然后在我的 XAML 中

<TabControl Height="101" Width="294">
    <TabItem Header="Resistance" IsSelected="{Binding SelectedTestStepForUI.vTestType, Converter={StaticResource EnumSelectedConverter}, ConverterParameter={x:Static local:TestType.LOWOHM}}">
    </TabItem>
    <TabItem Header="DC Hipot" IsSelected="{Binding SelectedTestStepForUI.vTestType, Converter={StaticResource EnumSelectedConverter}, ConverterParameter={x:Static local:TestType.DCW}}">
    </TabItem>
</TabControl>

注意:将其添加到 XAML 文件的顶部也像这样

<Window.Resources>
    <local1:EnumSelectedConverter x:Key="EnumSelectedConverter" />
</Window.Resources>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多