Question

I am modifying existing WPF code. The code , the xaml file, uses a DataGrid as opposed to DataGridView.

I need to use CellValueChanged event instead of CellEditEnding. Thanks.

<UserControl x:Class="myclass.view"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
         <Grid x:Name="MainGrid">
         <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <!--<RowDefinition Height="*"/>-->
    </Grid.RowDefinitions>

    <Grid Row="0" >
        <Border Background="Transparent">

            <Border.ContextMenu> 
                <ContextMenu>
                    <MenuItem Header="New Name" Command="{Binding Path=CreateNewNameCommand}"/>
                    <MenuItem Header="Delete Name" Command="{Binding Path=DeleteNameCommand}"/>
                </ContextMenu>
            </Border.ContextMenu>                                               
            <DataGrid
  Name="dataGrid1"
    ItemsSource="{Binding SelectedFolderPlane.FolderPlaneItems}"
    SelectedItem="{Binding SelectedGridItem}" 
    vw:DataGridDoubleClick.DoubleClickCommand="{Binding FolderPlaneItemDoubleClickCommand}"
     CellEditEnding="dataGrid1_CellEditEnding"
    SelectionMode="Single"
    SelectionUnit="FullRow"
    AutoGenerateColumns="False"
    BorderBrush="White"
    GridLinesVisibility="None"
    HeadersVisibility="Column"
    IsReadOnly="False"
    HorizontalAlignment="Left"
    VerticalAlignment="Top"
    HorizontalContentAlignment="Stretch"
    VerticalContentAlignment="Stretch">


        <DataGrid.Columns>
            <DataGridTemplateColumn Width="SizeToHeader" Header="" IsReadOnly="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Width="16" Height="16" Source="{Binding MyIcon, Mode=OneTime}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Width="*" Binding="{Binding Name, Mode=OneTime}" Header=" Name" IsReadOnly="False" >
                        <DataGridTextColumn.CellStyle>
                            <Style TargetType="DataGridCell">
                                <Setter Property="ToolTip" Value="{Binding ToolTipText}" />
                            </Style>
                        </DataGridTextColumn.CellStyle>
                    </DataGridTextColumn>

            <DataGridTextColumn Width="SizeToHeader" Binding="{Binding LatestMod, Mode=OneTime}" Header=" Version" IsReadOnly="True"/>
            <DataGridTextColumn Width="SizeToHeader" Binding="{Binding Date, Mode=OneTime}" Header="  LatestMod" IsReadOnly="True"/>
            </DataGrid.Columns>

                        <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding CanShowCatContextMenu}" Value="True">
                <Setter Property="ContextMenu" Value="{StaticResource Condition1ContextMenu}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding CanShowWFContextMenu}" Value="True">
                <Setter Property="ContextMenu" Value="{StaticResource Condition1ContextMenu}"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    </DataGrid.RowStyle>-->

    </DataGrid>
        </Border>
    </Grid>


</Grid>

No correct solution

OTHER TIPS

Hopefully this does what you intend (I'm still not totally sure, your first column is an icon, why would that change?)

First, change your Mode to either TwoWay or OneWayToSource. This allows changes in the cell to automatically update the underlying model (this may "commit" the changes unless you are bound to a database). The difference between the two is that with TwoWay, the view will change if the underlying model is modified, OneWayToSource will not.

Second, set the UpdateSourceTrigger to "PropertyChanged", which will cause the source to be updated every time the value changes (instead of requiring it to lose focus).

"{Binding Path=MyProperty, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"

Finally, put the code you want run (this could also do your "commit" step) in the setter of the bound property (or have it call a function that does so).

public String MyProperty
{
   get { return _backField; }
   set
   {
      _backField = value;
      BuisnessLogicAndCommit();
   }
}

Please let me know if there is any other information I can provide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top