WPF DataBinding Combobox绑定并不总是发生

时间:2013-09-23 09:20:43

标签: c# wpf data-binding datagrid combobox

我在DataGrid中有一个UserConrtol,其中有5个cols - 1个标题(只读),3个文本框和& 1个组合框。我将DataGrid绑定到ObservableCollection。我面临的唯一问题是: 如果我仅更改行的ComboBox,则ComboBox“单元”的属性绑定不会更新。如果我改变其他col&的价值也ComboBox,然后更新。
但是,如果我仅从列表中更改ComboBox项并从网格/行中退出,则ComboBox属性不会更新。我尝试了不同的方法,但无法找到上述原因。代码是:

<DataGrid AutoGenerateColumns="False" Height="133" Name="dgvData" Width="550" BorderBrush="#FFB7B39D" 
                  Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" 
                  CanUserReorderColumns="False" CanUserSortColumns="False" CanUserDeleteRows="False"
                  FontSize="13" CanUserAddRows="False" ItemsSource="{Binding Step3ModelList, Mode=TwoWay}">              
     <DataGrid.Columns>
         <DataGridTextColumn Header="" Binding="{Binding SqueezeHeader, ValidatesOnExceptions=True}" MinWidth="110" IsReadOnly="True"/>
         <DataGridTextColumn Header="Preflush" Binding="{Binding Preflush, ValidatesOnExceptions=True}" MinWidth="110"/>
         <DataGridTextColumn Header="Main Injection" Binding="{Binding MainInjection, ValidatesOnExceptions=True}" MinWidth="110"/>
         <DataGridTextColumn Header="Overflush" Binding="{Binding Overflush, ValidatesOnExceptions=True}" MinWidth="110"/>
         <DataGridTemplateColumn x:Name="reasonColumn" Header="Unit" Width="Auto" >
               <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                       <!-- ComboBox ItemsSource="{Binding Path=DataContext.UnitItems, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding Unit, Mode=TwoWay}" Text="Unit" /  -->

                      <ComboBox ItemsSource="{Binding Path=DataContext.UnitItems, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding Unit, ValidatesOnExceptions=True}" >
                      </ComboBox>
                  </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>

    <DataGrid.RowValidationRules>
           <validator:SqueezeEntryValidation ValidationStep="UpdatedValue" />
    </DataGrid.RowValidationRules>
    <DataGrid.RowValidationErrorTemplate>
        <ControlTemplate>
            <Grid Margin="0,-2,0,-2" ToolTip="{Binding RelativeSource={RelativeSource
                                                FindAncestor, AncestorType={x:Type DataGridRow}},
                                                            Path=(Validation.Errors)[0].ErrorContent}">
                <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}" Height="{TemplateBinding FontSize}" />
                <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center"  />
            </Grid>
        </ControlTemplate>
    </DataGrid.RowValidationErrorTemplate>

 </DataGrid>

实现INotifyPropertyChanged

的对象的单位属性
    public string Unit
    {
        get { return unit; }
        set
        {
            if (unit != value)
            {
                unit = value;
                Changed("Unit");
            }
        }
    }

Validaion Rule

public class SqueezeEntryValidation : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        Step3Model model = (value as BindingGroup).Items[0] as Step3Model;

        if (model.SqueezeHeader == null || model.SqueezeHeader == string.Empty)
            return new ValidationResult(false, "Squeeze Header can not be empty");
        if (model.Preflush < 0)
            return new ValidationResult(false, "Preflush can not be negative");
        if (model.Overflush < 0)
            return new ValidationResult(false, "Overflush can not be negative");
        if (model.MainInjection < 0)
            return new ValidationResult(false, "MainInjection can not be negative");

        return ValidationResult.ValidResult;
    }
}

我也尝试在Validate上调用ComboBox,以便更新结果,但Validate值在更改时永远不会调用ComboBox

我如何确保ComboBox的单位属性始终更新 - 如果有任何TextBox更新。用户单击Tab或以任何方式失去焦点或用户执行任何操作,如果值已更改,则必须更新。

任何人都可以帮助我。我错过了什么或出错了什么?

1 个答案:

答案 0 :(得分:2)

尝试将UpdateSourceTrigger=PropertyChanged添加到SelectedValue-binding。

<ComboBox ItemsSource="{Binding Path=DataContext.UnitItems, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding Unit, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" >
</ComboBox>