使用Itemscontrol datatemplate中的FindAncestor查找datatemplate之外的文本块

时间:2012-11-21 15:28:07

标签: wpf relativesource findancestor

我有一个ItemsControl绑定到一个对象,在ItemsControl的datatemplate中有两个文本块,我想将第一个textblock的text属性绑定到另一个位于这个ItemsControl之外的文本块。

我尝试在父datacontext中找到对象,并且只是尝试使用Path = Text找到TextBlock

下面是一个例子:

 <TextBlock Name="Name" Text="{Binding Name}"                                                            
     Grid.Column="0"   
     FontSize="{DynamicResource SmallSize}"
     TextWrapping="Wrap"
     TextAlignment="Right"
     Padding="4,0,0,0"
     Grid.ColumnSpan="2" Background="Aqua"/>

     <ItemsControl ItemsSource="{Binding TheValue}"                                                  
         Padding="4,0,0,0" 
         Grid.Column="2"  
         HorizontalAlignment="Right">

         <ItemsControl.ItemTemplate>
             <DataTemplate>
                 <WrapPanel>
                     <TextBlock Text = "{
                           Binding RelativeSource = 
                               {RelativeSource FindAncestor, 
                                AncestorType={x:Type Window}}, Path=Name}"                                                                                                            
                           Grid.Column="0"
                           FontSize="{DynamicResource SmallSize}"
                           TextWrapping="Wrap" ........................

1 个答案:

答案 0 :(得分:4)

{Binding RelativeSource = {RelativeSource FindAncestor,
                           AncestorType={x:Type Window}}, Path=Name}

在这里,您要告诉WPF使用类型窗口查找此控件的第一个父级,例如这是“ParentWindow”。此绑定发生在“ParentWindow”Name属性之后。

如果要在同一个XAML中定义的启用绑定控件,可以使用Binding.ElementName属性显式设置源。 这是代码的示例:

<TextBlock Text = "{Binding ElementName=Name, Path=Text}"/>

顺便说一句,使用控件名称作为“名称”不是很好。如果您使用此控件表单代码,它看起来像Name.Text =“some text”,这可能会导致很难理解正在发生的事情。

<强>更新 绑定到控制不同datatemplate中的DataContext属性的示例

class MainViewModel
{
    public Class1 C1 { get; set; }
    public Class2 C2 { get; set; }

    public MainViewModel()
    {
        C1 = new Class1 { S1 = "This is C1 data context" };
        C2 = new Class2 { S2 = "This is C2 data context" };
    }
}

在XAML中:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"        
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MainViewModel}">
            <StackPanel>
                <ContentControl Name="cc1" Content="{Binding C1}"/>
                <ContentControl Name="cc2" Content="{Binding C2}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Class1}">
            <TextBlock Text="{Binding S1}"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:Class2}">
            <TextBlock Text="{Binding ElementName=cc1, Path=DataContext.C1.S1}"/>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <ContentControl Content="{Binding}"/>
    </Grid>
</Window>

但是,我不认为这样的事情是一个好方法。特别是,因为这可能是使用此DataTemplate的许多项目。也许你需要将它委托给你的ViewModel。

相关问题