0

UserControl オブジェクトを作成していますが、Binding (PropertyChanged を使用) を使用して値を割り当てようとしています。プロトタイプを作ったのですが、ViewModel に値を代入すると値が表示されなかったり、UserControl コンポーネントを変更したりしませんが、表示されている UserControl オブジェクトに直接値を代入すると、変更が機能します。ウィンドウにオブジェクトを追加して直接バインドするだけで問題なく動作するため、何が間違っているのかを理解したいと思います (再度、PropertyChanged を使用)。以下のコードに従ってください。

助けてくれてありがとう。

よろしくお願いします、

グスタボ。

ユーザーコントロール:

<UserControl x:Class="WpfControlLibrary1.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="30" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>

ユーザー コントロールのコード ビハインド:

    public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    #region Title Property

    public static String GetTitle(DependencyObject obj)
    {
        return (String)obj.GetValue(TitleProperty);
    }

    public static void SetTitle(DependencyObject obj, String value)
    {
        obj.SetValue(TitleProperty, value);
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached(
            "Title",
            typeof(String),
            typeof(UserControl1),
            new FrameworkPropertyMetadata(TitleChangedCallback)
            );

    private static void TitleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UserControl1 _this = (d as UserControl1);
    }

    private String title;
    public String Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChanged("Title");
        }
    }

    #endregion

    #region INotifyPropertyChanged event and method

    public event PropertyChangedEventHandler PropertyChanged;

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion

** マイ ウィンドウ: **

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <uc:UserControl1 Title="{Binding TitleVM, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>

** 私の分離コード/ViewModel: **

public partial class MainWindow : INotifyPropertyChanged
{
    // Notify WPF that Counter changed
    public event PropertyChangedEventHandler PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();

        TitleVM = "açsldkfjasçldkfj";
    }

    private String titleVM;
    public String TitleVM
    {
        get { return titleVM; }
        set
        {
            titleVM = value;
            OnPropertyChanged("TitleVM");
        }
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

}
4

2 に答える 2

2

Windowを持っていませんDataContext。バインディングを解決できません。

これを試して:

public MainWindow()
{
    InitializeComponent();

    DataContext = this; //This is what you're missing!

    TitleVM = "açsldkfjasçldkfj";
}

編集:

あなたも同じことを見逃していますUserControl

public UserControl1()
{
    InitializeComponent();
    DataContext = this;
}
于 2013-03-08T21:31:34.803 に答える
0

HighCoreの助けを借りて、私は1つの問題を見つけました、そしてもう1つの問題は、私のUserControlに名前が定義されていないことでした。

簡単に言えば:

x:Name="UserControl"

UserControlのXAMLに変換され、機能します。また、コードを次のように変更しました。

        public String Title
    {
        get { return (String)base.GetValue(TitleProperty); }
        set { base.SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
     DependencyProperty.RegisterAttached(
         "Title",
         typeof(String),
         typeof(UserControl1),
         new FrameworkPropertyMetadata(null)
         );

私たちの目にはきれいなコード。

PS:置く必要はありません:

DataContext = this;

UserControlの背後にあるコードについて。少なくともここではバグの結果のみであり、値は来ていませんでした。

助けてくれてありがとうHighCore。

于 2013-03-11T18:07:48.997 に答える