gpt4 book ai didi

c# - 从 ControlTemplate 绑定(bind)到 ViewModel

转载 作者:行者123 更新时间:2023-11-30 21:53:44 26 4
gpt4 key购买 nike

我已将我的问题简化为一个最小的示例:我的 DataGrid 由 ControlTemplate 定义,在该 ControlTemplate 中我无法绑定(bind)到我的 ViewModel 中的任何内容。

这是简化的 ViewModel:

public class ViewModel : PropertyChangedBase
{
private string text1;

public ViewModel()
{
this.text1 = "tx1!";
}

public string Text1
{
get { return text1; }
set
{
if (value == text1) return;
text1 = value;
NotifyOfPropertyChange(() => Text1);
}
}
}

我尝试使用多种方法绑定(bind)到 Text1。我通过 Google 找到的大多数东西都建议使用 FindAncestor,但它也不起作用:

<Window x:Class="DataGridTemplateBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridTemplateBinding"
Title="MainWindow" Height="350" Width="525"
x:Name="LayoutRoot"
>
<Window.Resources>
<ControlTemplate x:Key="TableTemplate">
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" />
<!-- by ElementName -->
<DataGridTextColumn Header="{Binding DataContext.Text1, ElementName=LayoutRoot}" />
<DataGridTextColumn Header="{Binding Text1, ElementName=LayoutRoot}" />

<!-- by Ancestor type (Window) -->
<DataGridTextColumn Header="{Binding DataContext.Text1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<DataGridTextColumn Header="{Binding Text1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />

<!-- by templated parent -->
<DataGridTextColumn Header="{Binding DataContext.Text1, RelativeSource={RelativeSource TemplatedParent}}" />
<DataGridTextColumn Header="{Binding Text1, RelativeSource={RelativeSource TemplatedParent}}" />

<!-- by self?? -->
<DataGridTextColumn Header="{Binding DataContext.Text1, RelativeSource={RelativeSource Self}}" />
<DataGridTextColumn Header="{Binding Text1, RelativeSource={RelativeSource Self}}" />

<DataGridTextColumn Header="{Binding Content.DataContext.Text1, RelativeSource={RelativeSource TemplatedParent}}" />
<DataGridTextColumn Header="{Binding Content.Text1, RelativeSource={RelativeSource TemplatedParent}}" />

<DataGridTextColumn Header="{Binding DataContext.Text1}" />
<DataGridTextColumn Header="{Binding Text1}" />

<DataGridTextColumn Header="Column End" />
</DataGrid.Columns>
</DataGrid>
</ControlTemplate>
</Window.Resources>
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<DataGrid Template="{StaticResource TableTemplate}" />
</Grid>
</Window>

模板中的列如何绑定(bind)到 ViewModel 中的任何内容?我无法想象这是不可能的。


为了完整起见,这些是记录到输出的错误。它们的顺序与 XAML 的顺序一致。

System.Windows.Data Error: 40 : BindingExpression path error: 'DataContext' property not found on 'object' ''DataGridTextColumn' (HashCode=28365320)'. BindingExpression:Path=DataContext.Text1; DataItem='DataGridTextColumn' (HashCode=28365320); target element is 'DataGridTextColumn' (HashCode=28365320); target property is 'Header' (type 'Object')

System.Windows.Data Error: 40 : BindingExpression path error: 'Text1' property not found on 'object' ''DataGridTextColumn' (HashCode=54811268)'. BindingExpression:Path=Text1; DataItem='DataGridTextColumn' (HashCode=54811268); target element is 'DataGridTextColumn' (HashCode=54811268); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=55195297); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=38845522); target property is 'Header' (type 'Object')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=2399386); target property is 'Header' (type 'Object')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=31265986); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=6837024); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=17422861); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Content.DataContext.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=32669489); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Content.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=22361045); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=7953856); target property is 'Header' (type 'Object')

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text1; DataItem=null; target element is 'DataGridTextColumn' (HashCode=65911433); target property is 'Header' (type 'Object')

最佳答案

DataGridTextColumn 设置 HeaderTemplate,如下所示

<DataGridTextColumn  >
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Text1}"></TextBlock>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>

关于c# - 从 ControlTemplate 绑定(bind)到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694091/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com