目录

WPF-解决加载顺序,Combox-增加属性,并关联text

目录

WPF 解决加载顺序,Combox 增加属性,并关联text

<ComboBox x:Name="camera"  Height="17" DisplayMemberPath="DisplayText" SelectedValuePath="Value" Margin="5,5,5,5"  SelectedIndex="0"  ></ComboBox>
<TextBox Height="17"  Margin="5,5,5,5" x:Name="res_h" Text="{Binding ElementName=camera,Path=SelectedItem.Value1}" TextChanged="Calc"></TextBox>
 <TextBox Height="17"  Margin="5,5,5,5" x:Name="res_w" Text="{Binding ElementName=camera,Path=SelectedItem.Value2}"  TextChanged="Calc" ></TextBox>
<TextBox Height="17"  Margin="5,5,5,5" x:Name="vof_h" TextChanged="Calc" ></TextBox>
<TextBox Height="17"  Margin="5,5,5,5" x:Name="size" Text="{Binding ElementName=camera,Path=SelectedItem.Pixelsize}"  TextChanged="Calc"></TextBox>
public MainWindow()
{
    InitializeComponent();
    dataGrid.DataContext = this;
    var items = new List<ComboBoxItem>
{
    new ComboBoxItem { DisplayText = "Basler ace Classic acA2000-340kc 200万", Value1 = 2046,Value2=1086,Pixelsize=0.00505},
    new ComboBoxItem { DisplayText = "Dalsa 2k线阵", Value1 = 2048,Value2=1,Pixelsize=0.014  },
    new ComboBoxItem { DisplayText = "Dalsa 4k线阵", Value1= 4096,Value2=1,Pixelsize=0.00704  },
    new ComboBoxItem { DisplayText = "Dalsa 8k线阵", Value1 = 8192,Value2=1,Pixelsize=0.0035  },
    new ComboBoxItem { DisplayText = "Dalsa 16k线阵", Value1 = 16384,Value2=1,Pixelsize=0.0035  },
};

    camera.ItemsSource = items;
        public class ComboBoxItem
        {
            public string DisplayText { get; set; } // 显示的文本
            public object Value1 { get; set; }      // 对应的值
            public object Value2 { get; set; }      // 对应的值
            public object Pixelsize { get; set; }
        }