首页 > 解决方案 > 奇怪的 WPF 绑定行为

问题描述

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

我有这个 WPF 用户控件:

XAML:

<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>

C#:

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;
        }
    }
}

主窗口 XAML:

<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>

C#:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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;
using Kadaster.NewsLetters.DomainModel;

namespace WpfApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string hoi => "yYy";

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

我期望的输出是:

yYy yYy XxX XxX

相反,我得到:

yYy XxX XxX

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

标签: c#wpfxamldata-binding

解决方案


TomerAgmon1 在评论中指出它是正确的,您更改DataContext了用户控件。要解决此问题,您可以使用例如RelativeSource.

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

我宁愿不硬编码 a 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>

推荐阅读