0

コンテンツ コントロールのコンテンツ プロパティにバインドするにはどうすればよいですか?
カスタム コントロールを作成しました:

      public class CustomControl 
        {
         // Dependency Properties
public int MyProperty
        {
            get { return (int)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(int), typeof(MainViewModel), new PropertyMetadata(0));
         }

ViewModel で、このカスタム コントロールのタイプのプロパティを作成しました。

    public CustomControl CustomControl { get; set; }

ビューでは、このプロパティをコンテンツ コントロールにバインドします。

     <ContentControl x:Name="Custom" Content="{Binding CustomControl}"></ContentControl>

コンテンツ コントロールのコンテンツ プロパティにバインドするにはどうすればよいでしょうか。

4

1 に答える 1

0
<ContentControl Content="{Binding ElementName=Custom, Path=Content}" />

これがどのような影響を与えるかはわかりませんが。UI要素がすでに親または類似のものを持っていることについて不平を言うのではないかと私は疑っています。

アップデート

あなたの質問を正しく理解していると思うなら、バインディングを使ってやりたいことができるとは思いません。これは、新しいコンテンツを VM のプロパティに設定できるように、コンテンツが変更されたときのコールバックを追加する代替手段です。

class CustomControl : Control
{
    static CustomControl()
    {
        ContentControl.ContentProperty.OverrideMetadata(typeof(CustomControl), new PropertyMetadata(null, UpdateViewModel));
    }

    private static void UpdateViewModel(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = d as CustomControl;
        var viewModel = control.DataContext as MyViewModel;
        viewModel.CustomControl = control;
    }
}

おそらく、エラー処理が必要になるでしょう。

于 2013-03-04T16:52:29.817 に答える