I have made a simple sample to just guide you how it can be done
Xaml code
<DataGrid x:Name="dataGrid" IsEnabled="True" CanUserAddRows="False" AutoGenerateColumns="False" Width="275" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="25"/>
<DataGridTextColumn Header="Weight" Binding="{Binding Weight}" Width="25"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" Width="25"/>
<DataGridTemplateColumn Header="Length" Width="25">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Length, UpdateSourceTrigger=LostFocus}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Length}" Value="0">
<Setter Property="BorderBrush" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Height" Binding="{Binding Height}" Width="25"/>
<DataGridTextColumn Header="Width" Binding="{Binding Width}" Width="25"/>
<DataGridTextColumn Header="X" Binding="{Binding X}" Width="25"/>
<DataGridTextColumn Header="Y" Binding="{Binding Y}" Width="25"/>
<DataGridTextColumn Header="Z" Binding="{Binding Z}" Width="25"/>
</DataGrid.Columns>
</DataGrid>
The code behind
public partial class MainWindow : Window
{
public ObservableCollection<Model> Source { get; set; }
public MainWindow()
{
InitializeComponent();
Source = new ObservableCollection<Model>
{
new Model {ID=1,Weight=3,Quantity=5,Length=11,Height=12,Width=0,X=1,Y=-1,Z=-1 },
new Model {ID=2,Weight=21,Quantity=23,Length=0,Height=23,Width=11,X=-1,Y=-1,Z=-1 }
};
dataGrid.ItemsSource = Source;
}
}
EDIT Just add the UpdateSourceTrigger
in the TextBox
within the DataTemplate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.