我有一个DataGrid
有UserConrtol
5 个列 - 1 个标题(只读)、3 个文本框和 1 个组合框。我有DataGrid
一个ObservableCollection
. 我面临的唯一问题是:如果我只更改行的属性绑定,ComboBox
则不会更新“单元”的属性绑定。ComboBox
如果我也更改了其他 col & of 的值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");
}
}
}
验证规则
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
我如何确保 Unit 属性ComboBox
始终更新 - 如果有TextBox
更新或不更新。用户以任何方式点击Tab
或失去焦点或用户所做的任何事情,如果值发生变化,那么它必须得到更新。
谁能帮我解决这个问题。我错过了什么或错了?