奇怪的WPF绑定行为

时间:2019-01-02 12:02:44

标签: c# wpf xaml data-binding

使用另一个绑定变量时,WPF中的UserControl绑定不起作用。 这是我的(简化)代码。

我有这个WPF用户控件:

XAML:

naturalSize

C#:

<UserControl x:Class="WpfApplication.BindingTest"
             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:local="clr-namespace:WpfApplication"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <TextBlock Text="{Binding Test}" />
</UserControl>

主窗口 XAML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication
{
    public partial class BindingTest
    {
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register(
                "Test", typeof(string),
                typeof(BindingTest)
            );

        public string Test
        {
            get => (string)GetValue(TestProperty);
            set => SetValue(TestProperty, value);
        }

        public BindingTest()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

C#:

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <local:BindingTest Test="{Binding hoi}" />
        <TextBlock Margin="5,0,0,0" Text="{Binding hoi}" />
        <local:BindingTest Test="XxX" />
        <TextBlock Margin="5,0,0,0" Text="XxX" />       
    </StackPanel>
</Window>

我期望的输出是:

  

yYy yYy XxX XxX

相反,我得到了:

  

yYy XxX XxX

我的BindingTest控件不打印yYy文本,而TextBlock则打印。 知道为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

TomerAgmon1在注释中指出它正确,即您更改了用户控件的DataContext。要解决此问题,您可以使用RelativeSource

<local:BindingTest Test="{Binding DataContext.hoi, RelativeSource={RelativeSource AncestorType=local:MainWindow, Mode=FindAncestor}}"/>

我不想对DataContext进行硬编码:

namespace WpfApplication
{
    public partial class BindingTest
    {
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register(
                "Test", typeof(string),
                typeof(BindingTest)
            );

        public string Test
        {
            get => (string)GetValue(TestProperty);
            set => SetValue(TestProperty, value);
        }

        public BindingTest()
        {
            InitializeComponent();
        }
    }
}

<UserControl x:Class="WpfApplication.BindingTest"
             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:local="clr-namespace:WpfApplication"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <TextBlock Text="{Binding Test, RelativeSource={RelativeSource AncestorType=local:BindingTest}}" />
</UserControl>

答案 1 :(得分:1)

您的UserControl中的绑定表达式不正确,您无法在UserControl中将DataContext设置为 this

1)删除DataContext分配:

public BindingTest()
{
    InitializeComponent();
}

2)在您的UserControl XAML中,将绑定替换为:

<TextBlock Text="{Binding RelativeSource={RelativeSource 
    Mode=FindAncestor, AncestorType=local:BindingTest}, 
    Path=Test}" />