[英]Bind Enum to TabControl Tab Headers and Selected Tab
我有一個要綁定到Tab控件的枚舉。 基本上,我希望枚舉成員名稱(描述)填充選項卡標題,並選擇所選的選項卡來設置枚舉值。
我要執行的操作是“選擇”選項卡,控制“枚舉”的值。 因此,當我選擇選項卡“ DC Hipot”時,它將枚舉的值設置為DCW;當我選擇“電阻”選項卡時,將枚舉的值設置為LOWOHM。 然后,我希望相反的情況成立,如果枚舉值更改,則選擇相應的選項卡。 因此,例如,如果枚舉值更改為LOWOHM,則所選選項卡將更改為“電阻”。
我已經閱讀了將TabControl綁定到枚舉的線程,但是它沒有提供完整的解決方案。 我看不到任何這些實際上與選項卡控件相關聯。
這是一個帶有單選按鈕的示例,以及一個我想對Tabs和Enum進行處理的整數。 簡單的WPF RadioButton綁定?
特定枚舉為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; }
}
我終於找到了使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.