I
I
Ilya2014-10-08 07:25:07
User interface
Ilya, 2014-10-08 07:25:07

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>

How can I prevent records from being edited if the dataPrig cell contains yesterday's date (that is, the difference between dataPrig and today's date is 1 day)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Glebov, 2014-10-08
@Zerpico

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;
        }

2. Resource from this converter
<Window.Resources>
        <wpfApplication1:DateToEnabledConverter x:Key="dateToEnabledConverter" />
    </Window.Resources>

3. Replace DataGridTextColumn, DataGridComboBoxColumn with your own DataGridTemplateColumn and add IsEnabled property binding
<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 question

Ask a Question

731 491 924 answers to any question