簡體   English   中英

在嵌套用戶控件中綁定DependencyProperty

[英]Binding DependencyProperty in nested user controls

我有這個問題:我做了2個用戶控件:NestedControl1和NestedControl2。 NestedControl2包含NestedControl1,而NestedControl1僅包含TextBlock。 我已將每個NestedControl * DataContext設置為Self,並為每個對象創建了一個依賴項屬性。 NestedControl1.MyText1和NestedControl2.MyText2。 然后,將NestedControl2.MyText1綁定到MyText2,將TextBlock.Text綁定到MyText1。

如果我在窗口上使用NestedControl2並將MyText2設置為任何值,它將無法正常工作。 但是,如果我直接在Window上使用NestedControl1,它確實可以工作。 關鍵是我想使MyText2的值到達NestedControl1內部的TextBlock.Text屬性。

代碼如下。怎么了? 任何想法?? 預先感謝tou的解答

NestedControl2代碼:

public partial class NestedControl2 : UserControl
{
    public NestedControl2()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyText2Property = DependencyProperty.Register(
        "MyText2", typeof(string), typeof(NestedControl2), new PropertyMetadata(default(string)));

    public string MyText2
    {
        get { return (string)GetValue(MyText2Property); }
        set { SetValue(MyText2Property, value); }
    }
}

NestedControl2 xaml:

<UserControl x:Class="TestNestedPropertiesWpf.NestedControl2"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
          DataContext="{Binding RelativeSource={RelativeSource Self}}"
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
    <testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}" />
</Grid>

NestedControl1代碼:

public partial class NestedControl1 : UserControl
{
    public NestedControl1()
    {
        InitializeComponent();

    }


    public static readonly DependencyProperty MyText1Property = DependencyProperty.Register(
        "MyText1", typeof(string), typeof(NestedControl1), new PropertyMetadata(default(string)));

    public string MyText1
    {
        get { return (string)GetValue(MyText1Property); }
        set { SetValue(MyText1Property, value); }
    }
}

NestedControl1 xaml:

<UserControl x:Class="TestNestedPropertiesWpf.NestedControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <TextBlock Text="{Binding MyText1}" 
               x:Name="textBlock" Foreground="Red" 
               Width="300" Height="100" Background="Black"></TextBlock>
</Grid>

最后是MainWindow.xaml:

<Window x:Class="TestNestedPropertiesWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
    Title="MainWindow" Height="350" Width="525"  DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
    <testNestedPropertiesWpf:NestedControl1 MyText1="WORKING"/>
    <testNestedPropertiesWpf:NestedControl2 MyText2="NOT WORKING"/>
</StackPanel>

這行不通:

testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}"

添加clr屬性以將值從一個DP傳遞到另一個DP:

  • 向NestedControl2添加.Net屬性“ MyBindableText”,使DP MyText1綁定到它。
  • 在MyText2 DP的注冊中添加一個OnPropertyChanged處理程序。 在此處理程序中,將DP的新值分配給“ MyBindableText”。如果您設置MyText2 =“ bla”,則該值將轉發到Mytext1,並將為DP的MyText1和MyText2進行設置。

我找到了解決方案。 NestedControl2擁有自己的DataContext,並且使用NestedControl1,擁有DataContext到自己,因此這兩個DataContext是不同的。 為了解決該問題,我修改了MyText1中聲明的綁定,如下所示:

<testNestedPropertiesWpf:NestedControl1 MyText1="{Binding RelativeSource={RelativeSource AncestorType={x:Type testNestedPropertiesWpf:NestedControl2}}, Path=MyText2}" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM