DataGrid设置Property但没有获得更改SelectedIndex的值

时间:2011-10-19 18:12:13

标签: c# wpf xaml data-binding datagrid

我在Datagrid中遇到了一个奇怪的问题。我正在使用带有DataBinding的WPF,我有一个DataGrid,还有一个页面中的3个按钮 - 保存,更新,取消 - 以及许多标签。当用户单击“更新”按钮时,这些标签将为这些标签启用编辑模式。

在编辑模式下,该用户无法更改该Grid的SelectedIndex(这是我的问题)。 我试图创建一个Property IsNotEditable,绑定到该DataGrid的IsEnabled =“”,但是如果它被禁用,则取消选择当前行。

我无法使用它,因为当用户点击“保存”时,我将保存所选行。 所以......我用这段代码创建了另一个属性PlacasSelectedItem和一个'Support'属性PlacasSelectedAux:

 public ConeSlab PlacasSelectedAux { get; set; }

    private ConeSlab placasSelectedItem;
    public ConeSlab PlacasSelectedItem
    {
        get { return placasSelectedItem; }
        set
        {
            if (CurrentEditMode == EditMode.View)
            {
                placasSelectedItem = value;
                PlacasSelectedAux = value;
                OnPropertyChanged("PlacasSelectedItem");

                if (PlacasSelectedItem != null)
                    PlacaQuenteIsChecked = StringUtil.ConvertYesNoToBoolean(PlacasSelectedItem.Slab.InfHotSlab);
                else
                    PlacaQuenteIsChecked = false;

                ExibeLaminadorDestino();
            }
            else if (CurrentEditMode != EditMode.View)
            {
                // if isn't in ViewMode, and if user clicks in another Row, will force previously row to be selected.
                placasSelectedItem = PlacasSelectedAux;
                OnPropertyChanged("PlacasSelectedItem");
            }
        }
    }

好的,现在奇怪的问题:它执行代码,我看到代码中的变化,但SelectedIndex不会改变!数据绑定不起作用!

这是我的装订:

<DataGrid SelectedItem="{Binding Path=PlacasSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

  1. 由于网格上的项目可能会发生变化,因此从UI到模型,访问(此时)的更安全方式是找到实际的行在控制上。 您可以尝试使用扩展方法,如下所示:

    public static DataGridRow GetSelectedRowFromGrid(this DataGrid myDataGrid)
    {
    
        return (DataGridRow)
           myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.SelectedItem);
    }
    
  2. 在我看来(希望我能正确理解您的应用工作流程),您的行数据必须绑定到模型,因此用户在更改行内容时,最多可以DataBinding处理链接模型对象下面的更改,在它之后,实际上唯一需要的是保存模型对象,因此不需要任何UI访问

  3. 希望这有帮助。

答案 1 :(得分:0)

好的,问题解决了。进入xaml的Datagrid:

IsHitTestVisible="{Binding Path=IsEditing, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBooleanConverter}}" 

创建静态资源:

   <Page.Resources>
    <ut:InverseBooleanConverter x:Key="InverseBooleanConverter" />

   <Page.Resources>

这将反转布尔值以禁用更改Datagrid行选择,使用此方法:

/// <summary>
/// Convert bool to !bool
/// </summary>
[ValueConversion(typeof(Boolean), typeof(Boolean))]
public class InverseBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        return !(Boolean)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

现在用户无法选择DataGrid中的任何其他行! 谢谢你的帮助:)