【发布时间】:2019-01-02 12:02:43
【问题描述】:
当使用另一个绑定变量时,我在 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 可以。 知道这是为什么吗?
【问题讨论】:
-
您已指定
BindingTest的DataContext始终为this。当您在MainWindow.xaml引用BindingTest时,它无法识别Propertyhoi,因为它是MainWindow.cs的一部分,而不是DataContext。解决此问题的一种方法是不将BindingTest设为自己的DataContext,并以不同的方式将TextBlock绑定到属性Test。你可以给UserControl一个x:Name,然后像这样绑定到它:`` -
顺便说一句,通过使用
snoop工具,您可能已经看到了绑定错误,并且您还可以识别DataContext每个元素有什么 -
谢谢。不过,我的实际情况要复杂一些,并且我的控件具有 DataContext=this 因为这似乎是使控件观察其代码隐藏属性的方法。我做错了什么吗?
-
我的控件是这样调用的:
然后使用嵌套的 ItemsControls 它呈现每个部门每种语言的文本编辑器。在不使用 DataContext=this 的情况下,我将如何绑定这些属性并更新 UI? -
我想一定有很多方法可以解决这个问题,但您可以使用
ElementName进行绑定,正如我所解释的那样。这样你就可以维护DataContext继承
标签: c# wpf xaml data-binding