WPF中DataGrid中的CellValueChanged事件等价物

时间:2014-03-03 17:56:59

标签: wpf events datagridview datagrid

我正在修改现有的WPF代码。代码xaml文件使用DataGrid而不是DataGridView。

我需要使用CellValueChanged事件而不是CellEditEnding。感谢。

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

1 个答案:

答案 0 :(得分:1)

希望这可以达到您的目的(我仍然不完全确定,您的第一列是一个图标,为什么会改变?)

首先,将您的模式更改为TwoWay或OneWayToSource。这允许更改单元格以自动更新基础模型(除非您绑定到数据库,否则这可能会“提交”更改)。两者之间的区别在于,使用TwoWay,如果底层模型被修改,视图将会改变,OneWayToSource将不会。

其次,将UpdateSourceTrigger设置为“PropertyChanged”,这将导致每次值更改时更新源(而不是要求它失去焦点)。

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

最后,在绑定属性的setter中放置您想要运行的代码(这也可以执行“提交”步骤)(或让它调用一个执行此操作的函数)。

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

如果我能提供任何其他信息,请告诉我。