我最近发现了 BindingGroups
,它看起来相当不错。
但是我无法让它在我的 ListView
中工作,它充满了 DataTemplates
中表示的数据。
(事先我只想说:对不起所有代码,只是为了让你更好地理解)
所以在 XAML 中它看起来像这样:
<StackPanel Name="stpData">
<StackPanel.BindingGroup>
<BindingGroup />
</StackPanel.BindingGroup>
<!-- This works nice! -->
<TextBox Text="{Binding Path=name}" />
<CheckBox IsChecked="{Binding Path=enable}" />
<!-- This doesn't work so nice... -->
<ListView Name="lvBools" ItemsSource="{Binding Path=myList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding bools}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
和代码隐藏:
public MainWindow()
{
InitializeComponent();
TestData Data = new TestData()
{
name = "Test",
enable = true,
myList = new List<TestData.myItem>
{
new TestData.myItem(true),
new TestData.myItem(true),
new TestData.myItem(false)
},
};
stpData.DataContext = Data;
stpData.BindingGroup.BeginEdit();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
stpData.BindingGroup.CommitEdit();
stpData.BindingGroup.BeginEdit();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
stpData.BindingGroup.CancelEdit();
stpData.BindingGroup.BeginEdit();
}
public class TestData
{
public class myItem
{
public myItem(bool b)
{
this.bools = b;
}
public bool bools { get; set; }
}
public string name { get; set; }
public bool enable { get; set; }
public List<myItem> myList { get; set; }
}
那么当我更改名称或按下启用 CheckBox 时发生了什么,这些更改在我按下 SaveButton (btnSave_Click) 之前不会提交(不包含在 XAML
中)
或者,如果我按下取消按钮,更改将“恢复”。
但是,如果我单击 ListView 中的一个复选框,这些更改将立即提交。
我自己的猜测:ListView(甚至可能是 DataTemplate
中的 StackPanel
)停止了 BindingGroup
继承链。但在那种情况下,我该如何包含它们?
编辑
我还尝试将新的 BindingGroup
添加到 DataTemplate:
<DataTemplate>
<StackPanel x:Name="SPpan">
<StackPanel.BindingGroup>
<BindingGroup Name="BGStackPanel" />
</StackPanel.BindingGroup>
<CheckBox IsChecked="{Binding Path=bools}" />
</StackPanel>
</DataTemplate>
现在,当我编辑 bools
(在我的 ListView
中单击它们)时,源不会更新,这很好,但现在我找不到办法保存(提交)数据。
我无法从代码隐藏访问 SPpan
和 BGStackPanel
。
但是我尝试了以下方法:
var tmp = (lvBools.View as GridView).Columns[0];
(tmp.CellTemplate.LoadContent() as StackPanel).BindingGroup.BeginEdit();
但也没有成功...
编辑 好的,所以编辑 II... 所以我猜源不更新的原因是因为我在模板上运行 Begin/CommitEdit,而不是在从模板创建的实际对象上运行。那么有人知道如何到达这些对象吗?
最佳答案
好的,感谢 E.L Dunn 先生我设法解决了这个问题!
所以在我的第一次编辑中,我建议我可以将 BindingGroup
添加到 DataTemplate
中的 StackPanel
,这有点像朝着正确的方向……
所有需要做的就是将 BindingGroupName
添加到 DataTemplate 的 CheckBox。
<StackPanel Name="stpData">
<StackPanel.BindingGroup>
<BindingGroup Name="bindingGroup"/>
</StackPanel.BindingGroup>
<!-- This works nice! -->
<TextBox Text="{Binding Path=name}" />
<CheckBox IsChecked="{Binding Path=enable}" />
<!-- Now this work nice as well! -->
<ListView Name="lvBools" ItemsSource="{Binding Path=myList}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding bools, BindingGroupName=bindingGroup}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
就这么简单!
关于c# - ListView 中的 BindingGroup 与 DataTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13529366/