DataGrid 设置属性但未获取值来更改 SelectedIndex

发布于 2024-12-11 03:39:10 字数 1653 浏览 0 评论 0原文

我在数据网格中遇到一个奇怪的问题。我将 WPF 与 DataBinding 一起使用,我有一个 DataGrid,页面中有 3 个按钮 - 保存、更新、取消 - 以及很多标签。当用户单击“更新”按钮时,这些标签将启用这些标签的编辑模式。

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

我无法使用它,因为当用户单击“保存”时,我将保存该选定行。 所以...我创建了另一个属性 PlacasSelectedItem 和一个“支持”属性 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}" />

感谢您的帮助!

I'm having a weird problem in a Datagrid. I'm using WPF with DataBinding, I have a DataGrid, and 3 buttons in a Page - Save, Update, Cancel - and a lot of labels. These labels, when user clicks the Update Button, will enable editing mode to these labels.

While in edit mode, that user can't change SelectedIndex of that Grid (that's my problem).
I've tried to create a Property IsNotEditable, Binding to IsEnabled="" of that DataGrid, but if it's disabled, the current row is unselected.

I can't use that, because when user clicks Save, i'll save that Selected Row.
So... I've created another Property PlacasSelectedItem, and a 'Support' Property PlacasSelectedAux, with this code:

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

Ok, now the weird problem: It executes the code, I see the change in the code, but the SelectedIndex doesn't change! The Databinding doesn't work!

Here is my Binding:

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

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-12-18 03:39:10
  1. 由于网格上的项目可能会发生变化,从 UI 到模型,访问您的(此时)的更安全方法是在控件上找到实际上的一行。
    您可以尝试使用扩展方法,如下所示:

    public static DataGridRow GetSelectedRowFromGrid(this DataGrid myDataGrid)
    {
    
        返回(数据网格行)
           myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.SelectedItem);
    }
    
  2. 在我看来(希望我正确理解您的应用程序工作流程),行上的数据必须绑定到模型,因此用户在更改行时内容,由 DataBinding 负责更改下面链接的模型对象,之后,您实际上需要的唯一事情就是保存您的模型对象,< em>所以没有任何 UI 访问权限需要。。

希望这有帮助。

  1. As Item on your grid can be a subject to change, more secure way to access your row (at this point), from UI to model, is to find a actually a row on the control.
    You can try to use an extension method, something like this :

    public static DataGridRow GetSelectedRowFromGrid(this DataGrid myDataGrid)
    {
    
        return (DataGridRow)
           myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.SelectedItem);
    }
    
  2. It seems to me (hope I right understand your app workflow) , that your data on row has to be binded to the model, so the user when changes a row content, it's up to DataBinding to take care of changing underneath linked model object, and after it, the only thing you will need actually, is to save your model object, so no any UI access needed.

Hope this helps.

披肩女神 2024-12-18 03:39:10

好的,问题解决了。进入 xaml 的数据网格:

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

创建静态资源:

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

   <Page.Resources>

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

/// <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();
    }
}

现在用户无法选择数据网格中的任何其他行!
感谢您的帮助 :)

ok, problem solved. Into the xaml's Datagrid:

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

Created a Static Resource:

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

   <Page.Resources>

This will inverse the Boolean value to disable changing Datagrid row selection, using this method:

/// <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();
    }
}

Now the user can't select any other row in the DataGrid!
Thanks for your help :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文