DataGrid 设置属性但未获取值来更改 SelectedIndex
我在数据网格中遇到一个奇怪的问题。我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于网格上的项目可能会发生变化,从 UI 到模型,访问您的行(此时)的更安全方法是在控件上找到实际上的一行。
您可以尝试使用扩展方法,如下所示:
在我看来(希望我正确理解您的应用程序工作流程),行上的数据必须绑定到模型,因此用户在更改行时内容,由
DataBinding
负责更改下面链接的模型对象,之后,您实际上需要的唯一事情就是保存您的模型对象,< em>所以没有任何 UI 访问权限需要。。希望这有帮助。
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 :
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.
好的,问题解决了。进入 xaml 的数据网格:
创建静态资源:
这将反转布尔值以禁用更改数据网格行选择,使用此方法:
现在用户无法选择数据网格中的任何其他行!
感谢您的帮助 :)
ok, problem solved. Into the xaml's Datagrid:
Created a Static Resource:
This will inverse the Boolean value to disable changing Datagrid row selection, using this method:
Now the user can't select any other row in the DataGrid!
Thanks for your help :)