ObservableCollection 不反映 DataGrid 中的更改
我已经以这种方式实现了 DataGrid:
<DataGrid
x:Name="MyDataGridFilter"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
x:Name="FilterTextCol01">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Column="0"
IsHitTestVisible="True"
Text="{Binding Path=FilterTextCol01}" />
<CheckBox
Grid.Column="1"
x:Name="FilterAktivTextCol01"
IsHitTestVisible="True"
IsChecked="{Binding Path=FilterAktivTextCol01}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
代码中的绑定是这样的:(
FilterItemsList = new ObservableCollection<DataGridFilterEntity>();
MyDataGridFilter.DataContext = FilterItemsList;
它是短路的) FilterItemsList 被实现为 INotifyPropertyChanged 类:
public class DataGridFilterEntity : INotifyPropertyChanged
使用成员 FilterTextCol01(当然):
public string FilterTextCol01
{
get { return _FilterTextCol01; }
set
{
_FilterTextCol01 = value;
Changed("FilterTextCol01");
}
}
一切正常。当我更改 FilterItemsList 时,DataGrid 会反映这些更改。 但是,当我在 UI(在 DataGrid 中)中进行一些更改时,ObservableCollection (FilterItemsList) 不会反映出来。
我搜索并尝试了几个小时,但没有找到任何解决方案。 有谁知道如何解决这个问题? 谢谢你!
I have implemented a DataGrid that way:
<DataGrid
x:Name="MyDataGridFilter"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
x:Name="FilterTextCol01">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox
Grid.Column="0"
IsHitTestVisible="True"
Text="{Binding Path=FilterTextCol01}" />
<CheckBox
Grid.Column="1"
x:Name="FilterAktivTextCol01"
IsHitTestVisible="True"
IsChecked="{Binding Path=FilterAktivTextCol01}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The binding in the Code goes this way:
FilterItemsList = new ObservableCollection<DataGridFilterEntity>();
MyDataGridFilter.DataContext = FilterItemsList;
(it is shorted)
FilterItemsList is implemented as an INotifyPropertyChanged clas:
public class DataGridFilterEntity : INotifyPropertyChanged
With the member FilterTextCol01 (of course):
public string FilterTextCol01
{
get { return _FilterTextCol01; }
set
{
_FilterTextCol01 = value;
Changed("FilterTextCol01");
}
}
Everything works fine. When I change the FilterItemsList the DataGrid refelcts these changes.
But when I make some changes in the UI (in the DataGrid) it isn't reflected by the ObservableCollection (FilterItemsList).
I searched and tried some hours but did not find any solution.
Does anyone know how to solve this?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要双向绑定。
例如,
You need TwoWay binding.
For example,
您对 GUI 进行了哪些更改?您是否正在更新
TextBox
的Text
并检查CheckBox
?如果是这样,同样的例子适用于我的情况。当我将注意力从文本框或复选框上移开时,我会收到更新的文本并在模型中检查布尔值。
What kind of changes are you doing to the GUI? Are you updating the
Text
of theTextBox
and checking theCheckBox
?If so the same example works in my case. I receive the updated text and checked boolean back in my model when I focus off the textbox or checkbox.