2

- - - -編集 - - -

だから、私は私のコードが正しいと思いました、そしてあなたのすべての答えからのコードスニペットも正しいと思いました。それをありがとう。私の問題は、私のdev-maschineが異なる動作をする.NET4.5を実行していることです!まったく同じプログラム(.NET4.0に対してコンパイルされたもの)は、.NET4.0のマシンでは正しく実行されますが、.NET4.5のマシンでは正しく実行されません。

だからここに私の改訂された質問があります。

- - - -編集 - - -

まず、コンボボックスをデータコンテキストに双方向でバインドする簡単な例を次に示します。

モデルを見る:

public class MainWindowViewModel
{
    public List<String> MyElements { get; set; }
    public string SelectedElement { get; set; }

    public MainWindowViewModel()
    {
        MyElements = new List<string>() {"a", "b", "c"};
        SelectedElement = "a";
    }
}

およびコードビハインド

private readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
    InitializeComponent();
    DataContext = _viewModel;
}

と私のxaml

<ComboBox
        ItemsSource="{Binding MyElements, Mode=OneWay}"
        SelectedItem="{Binding SelectedElement}" />

これは正常に機能し、コンボボックスでアイテムを選択すると、ビューモデルにバインドされます。

OK、今度はviewModelを静的にしたいのですが、それでもselectedItemを双方向でバインドします。私はこれを試してみます:

public class MainWindowViewModel
{
    public static List<String> MyElements { get; set; }
    public static string SelectedElement { get; set; }

    static MainWindowViewModel()
    {
        MyElements = new List<string>() {"a", "b", "c"};
        SelectedElement = "a";
    }
}

コードビハインドでデータコンテキストを設定する必要はもうありません。xamlには双方向バインディング用のインスタンスが必要であることがわかっているので、デフォルトのコンストラクターを使用しています。次に、コンボボックスをバインドします

<Window.Resources>
    <me:MainWindowViewModel x:Key="model"/>
</Window.Resources>

<StackPanel>
    <ComboBox
        ItemsSource="{Binding Source={x:Static me:MainWindowViewModel.MyElements}, Mode=OneWay}"
        SelectedItem="{Binding Source={StaticResource model}, Path=SelectedElement}" />
</StackPanel>

初期値は適切にバインドされていますが、コンボボックスで別のアイテムを選択すると、viewModelに反映されません。私は何が間違っているのですか?

編集:

TextBoxにまったく同じバインディング文字列を使用し、ボックス内のテキストを変更すると、プロパティに反映されます。

<TextBox Text="{Binding Source={StaticResource model}, Path=SelectedElement}"/>

したがって、明らかに私のバインディング文字列は正しいですが、コンボボックスの使用方法が間違っているようです。SelectedValue私も代わりにバインドしようとしました...変更もありません。

4

2 に答える 2

4

サンプルプロジェクトでチェックインしただけで、正常に動作します

public class ViewModel
{
    static ViewModel()
    {
        Items=new ObservableCollection<string>();
        SelectedItem = "222";
        Items.Add("111");
        Items.Add("222");
        Items.Add("333");
        Items.Add("444");
        Items.Add("555");
    }
    private static string _selectedItem;
    public static string SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value;
            MessageBox.Show("Item " + value + " was selected");
        }
    }

    private static ObservableCollection<string> _items;
    public static ObservableCollection<string> Items
    {
        get { return _items; }
        set { _items = value; }
    }
}

およびxaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="200" Width="300">
<Grid>
    <Grid.Resources>
        <my:ViewModel x:Key="viewM"/>
    </Grid.Resources>
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="101,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="146" 
               ItemsSource="{Binding Source={x:Static my:ViewModel.Items}, Mode=OneWay}"
              SelectedItem="{Binding Source={StaticResource viewM}, Path=SelectedItem}" />
    </Grid>
</Window>

サンプルをアップロードしました。

于 2012-07-13T11:46:56.400 に答える
2

インスタンスと静的プロパティの間で混乱しています。静的オブジェクトをバインドする必要はありません。

<ComboBox
        ItemsSource="{x:Static me:MainWindowViewModel.MyElements}"
        SelectedItem="{x:Static me:MainWindowViewModel.SelectedElement}" />

INotifyPropertyChangedそれでも、実装する必要があります。

バインディングとは、データをフェッチする適切なインスタンスを解決することです。
インスタンスの意味がない場合は、バインドする必要はありません。

于 2012-07-13T10:54:15.423 に答える