English 中文(简体)
数据集
原标题:DataGridComboBoxColumn binding issue

I have DataGridComboBoxColumn in DataGrid:

  <DataGridComboBoxColumn Header="{x:Static Properties:Resources.Unit}" Width="Auto"

                                    SelectedValueBinding="{Binding UnitUnitId}"
                                    SelectedValuePath="UnitId"
                                    DisplayMemberPath="Name" >
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Units,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Units,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>

            </DataGridComboBoxColumn>

和ComboBox:

<ComboBox x:Name="unitBox" ItemsSource="{Binding Path=Units}" 
              SelectedItem="{Binding TaxSubGroup.Unit}"
               Grid.Row="2"
              Grid.Column="1" Margin="11,11,0,0" HorizontalAlignment="Stretch">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Converter={x:Static c:UnitToStringConverter.Default}}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

单位财产:

   public ObservableCollection<Unit> Units
    {
        get
        {
            return _units;
        }
        set
        {
            if(_units!=value)
            {
                _units = value;
                RaisePropertyChanged("Units");
            }
        }
    }

i m 改变ComboBoxes值时,数据GridComboBoxColum 自动更新其价值,但数据Grid s combo Boxcolumn的数值没有更新ComboBoxes值。 为什么?

附文

问题在于数据GridComboBoxCulumn s SelectedValueBled property。 所需要的是增加更新财产:

SelectedValueBinding="{Binding UnitUnitId,UpdateSourceTrigger=PropertyChanged}"
问题回答

您是否更新了<代码>Tax Subgroup.Unit in the setter of UnitUnitId property?

In your DataContext model class do something similar to this...

 public int UnitUnitId
 {
     get 
     {
         return this.unitID; 
     }
     set 
     {
         this.unitID = value;
         this.TaxSubGroup.Unit = Units.FirstOrDefault(u => u.UnitID == value);
         this.NotifyPropertyChange("UnitUnitId");
     }
 }

This will match SelectedItem of Combobox by reference.





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签