XAML에 바인딩하려는 일부 속성이있는 UserControl이 있습니다.UserControl 데이터 바인딩 속성이 작동하지 않습니다.
<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));
}
}
내가 그렇게
<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"/>
물론
같은 UserControl에 포함 된 창을 가지고, 실제 윈도우가 표시되지 않습니다 배경이 비어 있고 Label.Content
이 null이지만 자동차 창에 이렇게 속성이 올바르게 설정되었습니다.
저는 지난 2 시간 동안이 작업을 망쳤습니다. 무엇이 잘못 됐는지 모릅니다.
편집 나는이
private string versionNumber;
public string VersionNumber { get { return this.versionNumber; }
set {
this.versionNumber = value;
OnPropertyChanged("VersionNumber");
}
}
을 시도하고 그것은 여전히,이 경우 레이블이 업데이트되지 않습니다 작동하지 않습니다. 내가 제대로하지 않은 느낌이
에게 제대로 설정되어 있지 않은 경우 때문에 데이터 컨텍스트 문제가 될 수 있습니다 속성 집합의 OnPropertyChange ("yourpropertyname")가 누락되었습니다. –
당신의 속성의 설정자를 public으로 변경하려고 시도하고 그 것이 문제를 해결하는지 확인하십시오. – har07
@ har07 setter는 공개되어 있고, 그 개인은 게터입니다. @MihaiHantea 업데이트보기, 여전히 'OnPropertyChanged()'를 호출해도 작동하지 않습니다. – ron975