私は再利用したいユーザーコントロールがありますが、ボタンの内容を変更する方法を見つけることができないようです。私は既に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));
}
}
私は上記のすべてのコードを使用していませんが、まだ動作していません。助けてください。ありがとう。
ようにINotifyPropertyChangedのとあなたのsomepageを継承する必要があります。また、私のサンプルでは、ユーザーコントロールを使用して1がSomepageであることを何卒ご了承下さい。 xaml、ボタン内容のテキストがSomepage.csコードから来るようになります。 – user742102