繁体   English   中英

UserControl DataBinding属性不起作用

[英]UserControl DataBinding properties not working

我有一个UserControl,它具有一些我希望绑定到XAML的属性。

<UserControl x:Class="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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" DataContext="{RelativeSource Self}">
<UserControl.Background>
    <ImageBrush ImageSource="{Binding BackgroundImage}" Stretch="UniformToFill" AlignmentX="Center" AlignmentY="Bottom"/>
</UserControl.Background>

<Grid Name="mainGrid">
    <Label Canvas.ZIndex="-1" Foreground="Gray" Content="{Binding VersionNumber}" Height="28" HorizontalAlignment="Left" Name="versionLabel" VerticalAlignment="Bottom" />
</Grid>
</UserControl>

和代码背后:

public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    public string VersionNumber { private get; set; }
    public ImageSource BackgroundImage { private get; set; }

    public UserControl1()
    {
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

}

我有一个包含UserControl的窗口,像这样

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SomeNameSpace"

        Title="MainWindow" 
        MinHeight="400" MinWidth="400" >
<local:UserControl1 BackgroundImage="images\background.png" VersionNumber="10"/>

当然,实际窗口不显示任何内容,背景为空白, Label.Content为空,但是“自动”窗口向我显示属性已正确设置,如下所示。 汽车

在过去2个小时左右的时间里,我一直在搞弄这个问题,我不知道出了什么问题。

编辑我已经尝试过

 private string versionNumber;
 public string VersionNumber { get { return this.versionNumber; } 
 set { 
      this.versionNumber = value; 
      OnPropertyChanged("VersionNumber"); 
  } 
}

而且它仍然不起作用,在这种情况下,标签不会更新。

结果

您应该使用此标签绑定:

Content={Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type wpfApplication1:UserControl1}}, Path=VersionNumber}

UserControl1是您的用户控件类的名称。 您必须指定名称空间和用户控件。

您可以保持私有状态,此绑定仍然可以使用。

编辑按照您的代码进行此演示:

//window.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpfApplication1="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <wpfApplication1:UserControl1 VersionNumber="10"/>
</Grid>

//的UserControl1

<UserControl x:Class="WpfApplication1.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:wpfApplication1="clr-namespace:WpfApplication1"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" DataContext="{RelativeSource Self}">
<UserControl.Background>
    <ImageBrush ImageSource="{Binding BackgroundImage}" Stretch="UniformToFill" AlignmentX="Center" AlignmentY="Bottom"/>
</UserControl.Background>

<Grid Name="mainGrid">
    <Label Canvas.ZIndex="-1" Foreground="Gray" Content="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type wpfApplication1:UserControl1}}, Path=VersionNumber}" 
           Height="28" HorizontalAlignment="Left" Name="versionLabel" VerticalAlignment="Bottom" />
</Grid>

UserControl1.cs

public partial class UserControl1 : UserControl, INotifyPropertyChanged
{
    private string _versionNumber;
    private ImageSource _backgroundImage;

    public UserControl1()
    {
        InitializeComponent();
    }

    public string VersionNumber
    {
        private get { return _versionNumber; }
        set
        {
            _versionNumber = value;
            OnPropertyChanged("VersionNumber");
        }
    }

    public ImageSource BackgroundImage
    {
        get { return _backgroundImage; }
        set
        {
            _backgroundImage = value;
            OnPropertyChanged("BackgroundImage");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

如@anees所建议,您还可以对VersionNumber和BackgroundImage使用依赖项属性:

public static readonly DependencyProperty VersionNumberProperty = DependencyProperty.Register(
        "VersionNumber", typeof (string), typeof (UserControl1), new PropertyMetadata(default(string)));

    public string VersionNumber
    {
        get { return (string) GetValue(VersionNumberProperty); }
        set { SetValue(VersionNumberProperty, value); }
    }

public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register(
        "BackgroundImage", typeof (ImageSource), typeof (UserControl1), new PropertyMetadata(default(ImageSource)));

    public ImageSource BackgroundImage
    {
        get { return (ImageSource) GetValue(BackgroundImageProperty); }
        set { SetValue(BackgroundImageProperty, value); }
    }

我觉得您没有在MainWindow中正确包含Usercontrol,在MainWindow中包含以下名称空间

xmlns:local="clr-namespace:YourNameSpace"

并添加您的Usercontrol,如下所示:

<local:UserControl1 x:Name="somename" .... />

可能是datacontext问题,因为如果该显示为null,则说明上下文设置不正确

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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