Answer the question
In order to leave comments, you need to log in
How to prohibit editing a record, under certain conditions in the binding?
Hello. I have a DataGrid like this:
<DataGrid ItemsSource="{Binding Plavkas}" Grid.Row="1" CanUserAddRows="False"
CanUserDeleteRows="False" SelectionMode="Single" SelectedItem="{Binding CurrentPlavka}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Плавка" Binding="{Binding plavka1}" />
<DataGridComboBoxColumn x:Name="catMetalColumn" Header="Категория" DisplayMemberPath="category" SelectedItemBinding="{Binding catMetal1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn x:Name="smenaColumn" Binding="{Binding smena}" Header="Смена"/>
<DataGridTemplateColumn x:Name="dataPrig" Header="Дата приготовление.">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Height="19" FontSize="11" Padding="5,0,0,0" Margin="5,0,5,0"
SelectedDate="{Binding dataPrig, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Loaded="ComboBox_Loaded"
SelectedDateChanged="DatePicker_SelectedDateChanged"
/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Answer the question
In order to leave comments, you need to log in
1. Converter from DateTime to bool, if value is yesterday's date, it will return false
public class DateToEnabledConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
if(value is Plavka)
return ((DateTime.Now - ((Plavkas) value).dataPrig).Days != 1);
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return null;
}
<Window.Resources>
<wpfApplication1:DateToEnabledConverter x:Key="dateToEnabledConverter" />
</Window.Resources>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding plavka1}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox IsEnabled="{Binding Converter= {StaticResource dateToEnabledConverter}}" Text="{Binding plavka1}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question