2011-05-06 2 views
1

재사용하고 싶은 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)); 
     } 
    } 

어느 시점에서 내가 위의 모든 코드를 사용하지는 않았지만 여전히 작동하지 않습니다. 도와주세요. 감사.

답변

1

버튼이 ContentText에 바인딩되어 있지만 DataContext가 없으므로 부모로부터 상속됩니다. 당신은 아마이 작업을 수행 할 수 :

<UserControl 
     x:Name="root" 
     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 DataContext="{Binding ElementName=root}"> 
     <StackPanel> 
      <Button x:Uid="btnLeft" Name="btnBindCmd" Height="23" Click="AddCommand" Content="{Binding ContentText, Mode=TwoWay}", DataContext="UserControl1"/> 
     </StackPanel> 
    </Grid> 
</UserControl> 

주 루트 GridUserControl 자체의 DataContext 세트가 있습니다. 따라서 Button에있는 BindingUserControl 인스턴스의 ContentText 속성을 해결하려고 시도합니다.

1

UserControl1.xaml.cs에서 this.DataContext = this;을 생성자의 InitializeComponent(); 아래에 쓰십시오.

이것은 UserControl1.xaml에서하는 것처럼 datacontext를 할당 할 수 없기 때문입니다. 켄트는 올바른 길을 보여줍니다.

+0

같은에서 INotifyPropertyChanged와 somepage을 상속해야하지만, 그것은 작동하지 않습니다 :(. 또한, 내 샘플에서 그 통지하십시오, 해당 UserControl을 사용하여 하나 Somepage이다. xaml 및 버튼 내용 텍스트가 Somepage.cs 코드에서 올 것입니다. 다른 제안 사항을 알려주십시오. 감사합니다. – user742102

1

당신은 그냥 위의 제안을 시도이

public class Subject :INotifyPropertyChanged 
{ 

}