簡體   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