재사용하고 싶은 usercontrol이 있지만 버튼의 내용을 변경하는 방법을 찾지 못하는 것 같습니다. 나는 이미 여기에서 수색 한 모든 것을 시도했다. 누군가가 무엇이 실종되었는지를 말할 수 있다면, 나는 하루 종일 이것을 알아 내려고 노력하면서 정말로 감사 할 것입니다.버튼 콘텐트 바인딩이있는 usercontrol
<UserControl x:Class="SamplePopupWPF.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<StackPanel>
<Button x:Uid="btnLeft" Name="btnBindCmd" Height="23" Click="AddCommand" Content="{Binding ContentText, Mode=TwoWay}", DataContext="UserControl1"/>
</StackPanel>
</Grid>
코드 사용자 제어 : 제어를 사용하여 Somepage은 "
<Grid >
<user:UserControl1 x:Name="btnTEST" AddCommandClick="onBtnClick" ContentText="{Binding OtherCmd, ElementName=UserControl1}" />
</Grid>
코드 : 여기
public string ContentText
{
get { return (string) GetValue(ContentTextProperty); }
set { SetValue(ContentTextProperty, value); }
}
public static readonly DependencyProperty ContentTextProperty = DependencyProperty.Register("ContentText", typeof (String), typeof (UserControl1),new IPropertyMetadata(String.Empty,textChangedCallback));
static void textChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UserControl1 c = (UserControl1) d;
c.ContentText = e.NewValue as String;
}
는 Somepage 컨트롤을 사용하고 여기에 내 코드입니다 :
public string OtherCmd
{
get { return _otherCmd; }
set
{
_otherCmd = value;
//if (this.PropertyChanged != null)
NotifyProperty("OtherCmd");
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
OtherCmd = "helloTest";
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyProperty(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
어느 시점에서 내가 위의 모든 코드를 사용하지는 않았지만 여전히 작동하지 않습니다. 도와주세요. 감사.
같은에서 INotifyPropertyChanged와 somepage을 상속해야하지만, 그것은 작동하지 않습니다 :(. 또한, 내 샘플에서 그 통지하십시오, 해당 UserControl을 사용하여 하나 Somepage이다. xaml 및 버튼 내용 텍스트가 Somepage.cs 코드에서 올 것입니다. 다른 제안 사항을 알려주십시오. 감사합니다. – user742102