繁体   English   中英

用户控件中的数据绑定不起作用 (WPF)

[英]Databinding in user control doesn't work (WPF)

我在 WPF 中有以下示例代码。 当我将 xaml 和相应的 C# 代码放入 windows 时,它运行良好,但是当我使用用户控件时,数据绑定不起作用。 为什么?

这是代码:

用户控制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>

用户控件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;
    }
}

主窗口 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>

我注意到,如果我将所有 C# 和 xaml 代码从用户控件移动到 MainWindows,则绑定正常工作。

代码有什么问题?

您将依赖属性注册为MainWindow而不是您的 UserControl。

public static readonly DependencyProperty IsEditingProperty =
    DependencyProperty.Register
    (
        "IsEditing",
        typeof(Boolean),
        typeof(MainWindow), // <-- Here
        new PropertyMetadata(false)
    );

那是因为您错误地注册IsEditingProperty 您必须为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM