1

ViewModel が関連付けられている WPF でカスタム UserControl を構築しています。また、コード ビハインドでコントロールを動的に作成したいと考えています。しかし、生成されたコントロールを ViewModel プロパティにバインドする際に問題が発生しています。私のコードは次のとおりです。

<UserControl x:Class="SVT.Teste.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="300" d:DesignWidth="300"
             DataContext="UserControl1ViewModel">
    <Grid Name="GridContainer">
    </Grid>
</UserControl>

コードビハインド:

    public UserControl1()
    {
        InitializeComponent();
        System.Windows.Controls.Button newBtn = new Button();
        newBtn.SetBinding(Button.ContentProperty, new Binding("Test"));
        GridContainer.Children.Add(newBtn);
    }

    public class UserControl1ViewModel
    {
        private string test = "ola";

        public string Test
        {
            get { return test; }
        }
    }

これを実行すると、次のようになります。

「System.Windows.Data エラー: 40: BindingExpression パス エラー: 'Test' プロパティが 'object' ''String' に見つかりません (HashCode=-946585093)'.BindingExpression:Path=Test; DataItem='String' (HashCode= -946585093); ターゲット要素は 'ボタン' (Name=''); ターゲット プロパティは 'コンテンツ' (タイプ 'オブジェクト')"

手伝って頂けますか?

4

4 に答える 4

2

DataContextを、プロパティを持つインスタンスではなく、タイプに設定しています。ユーザーコントロールを作成するメソッドでは、次のようにします。

          public UserControl1()
        {
            InitializeComponent();
            System.Windows.Controls.Button newBtn = new Button();
            newBtn.SetBinding(Button.ContentProperty, new Binding("Test"));
            GridContainer.Children.Add(newBtn);
            **DataContext = new UserControl1ViewModel();**
        }

まだやるべきことがたくさんあります。通知や更新がない方法で発生します。インターフェイスを実装しINotifyPropertyChangedます(on UserControlViewModel)。そして、タイプDataContextの設定を削除します。XAML

于 2012-07-06T07:32:52.007 に答える
2

ビュー モデル インスタンスではなく、文字列DataContextのプロパティを設定しています。UserControl1

次のようなことをする必要があります:

<UserControl xmlns:local="clr-namespace:NAMESPACE_WHERE_VIEWMODEL_IS_DEFINED">
    <UserControl.DataContext>
        <local:UserControl1ViewModel />
    </UserControl.DataContext>
    <!-- unrelated code omitted -->
</UserControl>
于 2012-07-06T07:26:33.233 に答える
1

このバインディングで試してください

Binding MyBinding = new Binding();
MyBinding.Path = new PropertyPath("Test");
newBtn.DataContext = new UserControl1ViewModel(); //or MyBinding.Source = new //UserControl1ViewModel();


newBtn.SetBinding(Button.ContentProperty, MyBinding);
于 2012-07-06T07:23:46.933 に答える
0

マックスは正しいですが、別の質問があります。バインドしたいviemwodelがあるのに、なぜユーザーコントロールを動的に作成したいのですか? 私には意味がありません。説明させてください:

ビューモデルがある場合-このビューモデルをレンダリングする方法とバインディングが何であるかを知っています。このビューモデルのユーザーコントロール/ビューを作成できます

MyUserControl1View.xaml

<UserControl>
 <Grid>
   <Button Content="{Binding Test}"/>
   <!-- more controls and binding if the viewmodel expose more-->
 </Grid>
</UserControl>

あなたが今持っているのは、ビューモデルの表現です。それらは接続されていませんが、ビューモデルは次のようになり、バインディングが設定されます。今まで、データコンテキストはまったく設定されていません。

あとは、viewmodel の最初のアプローチと datatemplate の使用を行うだけです。

メインウィンドウで次のことを想定しましょう

<Window>
 <Window.Resources>
  <DataTemplate Datatype="{x:Type local:Usercontrol1viewmodel}">
    <view:MyUserControl1View/>
  </DataTemplate>
 </Window.Resources>

これで、wpf は Usercontrol1viewmodel をレンダリングする方法を認識しました。

メインウィンドウのビューモデルでさらに一歩、Usercontrol1viewmodel を処理します。

 public Usercontrol1viewmodel MyWhatEver {get;set;} 

このプロパティを contentpresenter にバインドすると、wpf マジックが表示されます;)

 <ContentPresenter Content="{Binding MyWhatEver}"/>

contentpresenter に MyUserControl1View が表示されるようになりました。動的ビュー コードは必要ありません。ビューモデルを使用するだけです。

ps: 私の下手な英語を自由に編集してください。

于 2012-07-06T07:40:32.783 に答える