Datagrid Combobox Seletected item Value to Textbox binding

时间:2015-09-18 01:37:02

标签: c# wpf xaml combobox datagrid

希望你表现得很好并且编码为Guru。

这是Datagrid绑定问题,这对我来说是痛苦的2天。我问了另外一个与这个问题有关的问题并解决了,但这里还有其他问题。

我有RawVal struct和Signal类(有ObservableCollection)。

public struct RawVal
{
    public string name { get; set; }
    public int value { get; set; }
}

public class Signal
{
    public string Name { get; set; }
    public Int32 Value { get; set; }
   public ObservableCollection<RawVal> rawValue { get; set; }
}

现在XAML看起来像

<DataGrid ItemsSource="{Binding}" Name="grdSignal" Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False" SelectionChanged="grdSignal_SelectionChanged_1">
     <DataGrid.Columns>
        <DataGridTextColumn Header="            Signal Name" Binding="{Binding Name}" Width="150"/>
           <DataGridTemplateColumn Header="   Physical Value " Width="100">
              <DataGridTemplateColumn.CellTemplate>
                 <DataTemplate>
                      <ComboBox ItemsSource="{Binding rawValue}" SelectedItem="Binding name"  DisplayMemberPath="name" Name="cmbVal" SelectedIndex="0" 
                             Visibility="{Binding Path=rawValue.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}"/>
                 </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="    Value " Width="100">
                <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                       <TextBox Text="{Binding }" />
                   </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
          </DataGridTemplateColumn>
        </DataGrid.Columns>
     </DataGrid> 

UI with binding

问题是当用户从Physical Value Combobox中选择任何项目时,与此项目相关的值需要显示在“值”文本框中。

例如,Signal类中的RawVal包含这些值。

4 - Search Key Trigger
3 - Tailgate Key Trigger
2 - Un-Lock Key Trigger 
1 - Lock Key Trigger
0 - No Remote RQ Trigger

当用户选择“Tailgate Key Trigger”时,文本框中会出现3。选择“No Remote RQ Trigger”时,为0.

欢迎任何建议和解决方案, 谢谢。

2 个答案:

答案 0 :(得分:2)

我已经创建了一个基于MVVM的简单解决方案,可以指导您如何完成。

ViewModel类

class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<Signal> _source;
    public IEnumerable<Signal> MySource
    {
        get { return _source; }
    }

    private RawVal _rawValSelected;
    public RawVal RawValSelected
    {
        get { return _rawValSelected; }
        set
        {
            _rawValSelected = value;

            RaisePropertyChanged("RawValSelected");
        }
    }

    public void RaisePropertyChanged(string propName)
    {
        var pc = PropertyChanged;
        if (pc != null)
        {
            pc(this, new PropertyChangedEventArgs(propName));
        }
    }

    public ViewModel()
    {
        _source = new ObservableCollection<Signal>
        {
            new Signal{
                Name = "Test",
                Value = 1,
                rawValue = new ObservableCollection<RawVal>
                {
                    new RawVal{name="Search Key Trigger",value=4},
                    new RawVal{name="Tailgate Key Trigger",value=3},
                    new RawVal{name="Un-Lock Key Trigger",value=2},
                    new RawVal{name="Lock Key Trigger",value=1},
                    new RawVal{name="No Remote RQ Trigger",value=0}
                }
            }
        };
    }
}

视图

<DataGrid ItemsSource="{Binding MySource}" Name="grdSignal" CanUserAddRows="False" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Signal Name" Binding="{Binding Name}" Width="150" />
        <DataGridTemplateColumn Header="Physical Value " Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding rawValue}"
                              SelectedItem="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},Path=DataContext.RawValSelected}"
                              DisplayMemberPath="name" SelectedIndex="0" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Value" Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},Path=DataContext.RawValSelected.value}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我删除了一些代码,因为我没有转换器代码 注意使用RelativeSource帮助我将组合框的选定值绑定到视图模型中的属性

视图背后的代码

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new ViewModel();
    }
}

答案 1 :(得分:0)

您必须为INotifyPropertyChanged实施Signal个事件,并使用SelectedItem的{​​{1}}属性设置Combobox的值 列。

Xaml代码:

TextBox

C#/代码隐藏

<DataGrid ItemsSource="{Binding}" Name="grdSignal" Grid.Row="1" CanUserAddRows="False" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="            Signal Name" Binding="{Binding Name}" Width="150"/>
        <DataGridTemplateColumn Header="   Physical Value " Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox  ItemsSource="{Binding rawValue, Mode=OneWay}" SelectedItem="{Binding SelectedRaValue, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}"  DisplayMemberPath="name" Name="cmbVal"
                             Visibility="{Binding Path=rawValue.Count, Converter={StaticResource ComboBoxItemCountToEnabledConverter}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="    Value " Width="100">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding ComboValue}"  />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

现在您可以设置public class Signal: INotifyPropertyChanged { public string Name { get; set; } public Int32 Value { get; set; } private ObservableCollection < RawVal > rawValue1; public ObservableCollection < RawVal > rawValue { get { return rawValue1; } set { rawValue1 = value; OnPropertyChanged("rawValue"); if (value != null && value.Count > 0) { SelectedRaValue = value.First(); } } } private RawVal selectedRaValue; public RawVal SelectedRaValue { get { return selectedRaValue; } set { selectedRaValue = value; OnPropertyChanged("SelectedRaValue"); ComboValue = value.name; OnPropertyChanged("ComboValue"); } } public string ComboValue { get; set; } #region Implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion }

DataContext

希望这会有所帮助!!