简体   繁体   English

将枚举绑定到TabControl选项卡标题和选定的选项卡

[英]Bind Enum to TabControl Tab Headers and Selected Tab

I have an Enum that I want to bind to a Tab Control. 我有一个要绑定到Tab控件的枚举。 Basically I want the Enum member names (Descriptions) to populate the tab headers and the selected Tab to set the Enum Value. 基本上,我希望枚举成员名称(描述)填充选项卡标题,并选择所选的选项卡来设置枚举值。

What I want to do is have the Selected tab, control the value of the Enum. 我要执行的操作是“选择”选项卡,控制“枚举”的值。 So when I select Tab "DC Hipot" it sets the value of the enum to DCW, When I select the "Resistance" Tab it sets the value of the enum to LOWOHM. 因此,当我选择选项卡“ DC Hipot”时,它将枚举的值设置为DCW;当我选择“电阻”​​选项卡时,将枚举的值设置为LOWOHM。 Then I want the reverse to be true, if the enum value changes then the corresponding tab is selected. 然后,我希望相反的情况成立,如果枚举值更改,则选择相应的选项卡。 So for example if the enum value changes to LOWOHM then the selected tab is changed to "Resistance". 因此,例如,如果枚举值更改为LOWOHM,则所选选项卡将更改为“电阻”。

I have read this thread Binding TabControl to an enum but it does not provide a complete solution. 我已经阅读了将TabControl绑定到枚举的线程但是它没有提供完整的解决方案。 I do not see where any of this actually ties in to the Tab Control. 我看不到任何这些实际上与选项卡控件相关联。

This is an example with radio buttons and an int of what I want to do with Tabs and an Enum. 这是一个带有单选按钮的示例,以及一个我想对Tabs和Enum进行处理的整数。 Simple WPF RadioButton Binding? 简单的WPF RadioButton绑定?

The Specific enum is MV_Main.SelectedTestStepForUI.vTestType. 特定枚举为MV_Main.SelectedTestStepForUI.vTestType。

Here is my MainWindows.xaml 这是我的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>

Here is the Main View Model, MV_Main and it's dependencies 这是主视图模型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; }
}

I finally found a solution which is to use a IValueConverter 我终于找到了使用IValueConverter的解决方案

Here is my converter code 这是我的转换器代码

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

Then in my XAML 然后在我的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>

Note: Make to to add it to the top of the XAML file also Like this 注意:也可以将其添加到XAML文件的顶部

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM