[英]Databinding in user control doesn't work (WPF)
I have the following sample code in WPF.我在 WPF 中有以下示例代码。 It is working well when I put the xaml and corresponding C# code inside windows, but when I am using a user control, the databinding doesn't work.当我将 xaml 和相应的 C# 代码放入 windows 时,它运行良好,但是当我使用用户控件时,数据绑定不起作用。 why?为什么?
Here is the code:这是代码:
User control XAML:用户控制XAML:
<UserControl x:Class="TestWpf.UserControl1"
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:ec="clr-namespace:TestWpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="MainGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<WrapPanel HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Row="0" >
<Button Content="Edit" HorizontalAlignment="Right" Name="editButton1" VerticalAlignment="Stretch" Click="button1_Click" />
</WrapPanel>
<Grid x:Name="patientDataGrid" Margin="10,10,10,10" Grid.Row="1">
<Grid.Resources>
<Style TargetType="TextBox" >
<Setter Property="IsEnabled" Value="{Binding Path=IsEditing, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ec:UserControl1}}}" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox x:Name="textBox1" Grid.Column="0" Text="!234" />
<TextBox x:Name="textBox2" Grid.Column="1" />
</Grid>
</Grid>
</UserControl>
The user control C# code:用户控件C#代码:
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
"IsEditing", typeof(Boolean), typeof(MainWindow), new PropertyMetadata(false));
public Boolean IsEditing
{
get { return (Boolean)GetValue(IsEditingProperty); }
set { SetValue(IsEditingProperty, value); }
}
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
IsEditing = !IsEditing;
}
}
MainWindows XAML:主窗口 XAML:
<Window x:Class="TestWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ec="clr-namespace:TestWpf"
Title="MainWindow" Height="200" Width="525">
<ec:UserControl1 x:Name="c" />
</Window>
I notice that if I move all C# and xaml code from user control to MainWindows, the binding is working without any problem.我注意到,如果我将所有 C# 和 xaml 代码从用户控件移动到 MainWindows,则绑定正常工作。
What is wrong with the code?代码有什么问题?
You registered the dependency property to be owned by MainWindow
instead of your UserControl.您将依赖属性注册为MainWindow
而不是您的 UserControl。
public static readonly DependencyProperty IsEditingProperty =
DependencyProperty.Register
(
"IsEditing",
typeof(Boolean),
typeof(MainWindow), // <-- Here
new PropertyMetadata(false)
);
That's because you're registering IsEditingProperty
incorrectly.那是因为您错误地注册IsEditingProperty
。 You have to register it for UserControl1
, not for MainWindow
:您必须为UserControl1
注册它,而不是为MainWindow
:
public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
"IsEditing", typeof(Boolean), typeof(UserControl1), new PropertyMetadata(false));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.