5

Window から派生した単純なビューがあります。その派生クラスのコード ビハインド ファイルで、ActiveDocument という名前の新しい DependencyProperty を定義します。

この新しい DependencyProperty を、ビューの DataContext として設定されている ViewModel のプロパティにバインドしたいと考えています。

クラス コンストラクターのコードを使用してこのバインディングを設定できますが、XAML ファイルでプロパティをバインドしようとすると、プロパティ ActiveDocument がクラス Window に見つからないというエラー メッセージが表示されます。

XAML でこれを行うための正しい構文は何ですか?

[コードで更新]

MainWindowViewModel.cs

class MainWindowViewModel
{
    public bool IWantBool { get; set; }
}

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = new MainWindowViewModel();
        InitializeComponent();
    }

    public static readonly DependencyProperty BoolProperty = DependencyProperty.Register(
        "BoolProperty", typeof(bool), typeof(MainWindow));
}

メインウィンドウ.xaml

<Window x:Class="DependencyPropertyTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DependencyPropertyTest"

    <!-- ERROR: BoolProperty not found on type Window. -->
    BoolProperty="{Binding path=IWantBool}"

    <!-- ERROR: Attachable property not found in type MainWindow. -->
    local:MainWindow.BoolProperty="{Binding path=IWantBool}">

    <Grid>

    </Grid>
</Window>
4

1 に答える 1

3

XAMLコンパイラは、の実際の型を考慮せずWindow、ルート要素の型のみを調べます。したがって、で宣言されたプロパティを認識しませんMainWindow。XAMLで簡単に実行できるとは思いませんが、コードビハインドで実行するのは簡単です。

public MainWindow()
{
    DataContext = new MainWindowViewModel();
    InitializeComponent();
    SetBinding(BoolProperty, new Binding("IWantBool"));
}
于 2012-05-10T16:26:27.790 に答える