2011-05-06 7 views
1

私は再利用したいユーザーコントロールがありますが、ボタンの内容を変更する方法を見つけることができないようです。私は既にjaveがここで検索したすべてを試しました。もし誰かが何が欠けているか教えてもらえれば、私はこれを一日中理解しようとしているので、本当に感謝しています。ここに私のコードは次のとおりです。ユーザーコントロールのボタンコントロールのバインディングを持つ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> 

コード:ここ

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です:」コントロールを使用してSomepageため

<Grid > 
      <user:UserControl1 x:Name="btnTEST" AddCommandClick="onBtnClick" ContentText="{Binding OtherCmd, ElementName=UserControl1}" /> 

    </Grid> 

コード:

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> 

UserControl自体にそのDataContextセットを持っていGridルート:あなたはおそらくこれをしたいです。したがって、ButtonBindingUserControlインスタンスのContentTextプロパティを解決しようとします。

1

コンストラクタのInitializeComponent();の下にUserControl1.xaml.csにthis.DataContext = this;と書いてください。

これは、UserControl1.xamlで行っているようにdatacontextを割り当てることができないためです。ケントは正しい方法を示しています。

+0

ようにINotifyPropertyChangedのとあなたのsomepageを継承する必要があります。また、私のサンプルでは、​​ユーザーコントロールを使用して1がSomepageであることを何卒ご了承下さい。 xaml、ボタン内容のテキストがSomepage.csコードから来るようになります。 – user742102

1

あなただけの:(私は上記の提案を試みたが、それは働いていないこの

public class Subject :INotifyPropertyChanged 
{ 

} 
関連する問題