c# - 奇怪的 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 则打印。知道这是为什么吗?
解决方案
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>
推荐阅读
- python - 使用 tf.data.map 将音频文件拆分为 1 秒的音频张量块?
- android - Android Detekt 构建失败
- python - 在循环中调用函数在 Python 中不起作用
- machine-learning - 为什么在 MNIST 分类器代码中使用 X[0] 会给我一个错误?
- ruby-on-rails - Ruby on Rails 5.2 - NoMethodError(nil:NilClass 的未定义方法“主机”):
- php - 使用按钮删除一行
- matrix - Power BI 矩阵 - 使用 DAX 减去两个(动态)对应列
- android-5.1.1-lollipop - 在 Delphi 10.4 上为 Android 5.1 开发?
- node.js - 如何从 kubernetes 应用程序在 kubernetes 中正确运行作业
- mongodb - docker容器内的mongod进程如何允许来自外部的连接