0

以下に示すコンボボックスがあります。コード ビハインドが常に起動しないのはなぜですか?

XAML:

<ComboBox   Height="23"
                        Name="cbAppendCreate"
                        VerticalAlignment="Top"
                        Width="120"
                        Margin="{StaticResource ConsistentMargins}"
                        ItemsSource="{Binding Path=CbCreateAppendItems}"
                        SelectedValue="{Binding Path=CbAppendCreate,UpdateSourceTrigger=PropertyChanged}" />

分離コード:

private string cbAppendCreate;
public string CbAppendCreate {
    get {
        //....
        return cbAppendCreate
    }
    set { //This doesn't fire when selecting the first of 2 Items, 
          //but always fires when selecting the 2nd of two items 
          //....
         cbAppendCreate = value;
    }
}
4

1 に答える 1

1

ここに作業コードを投稿します。非常に簡単です。VS2012 テンプレートを使用してデフォルトの WPF アプリを作成しました。MainWindow.xaml の内容は次のとおりです。

<Window x:Class="
    WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ComboBox   Height="23"
                    Name="cbAppendCreate"
                    VerticalAlignment="Top"
                    Width="120"
                    ItemsSource="{Binding Path=CbCreateAppendItems}"
                    SelectedValue="{Binding Path=CbAppendCreate,UpdateSourceTrigger=PropertyChanged}" />
    <TextBlock Text="{Binding CbAppendCreate}"></TextBlock>
</StackPanel>

コードビハインドは次のとおりです。

namespace WpfApplication1
    {
    public class DataSource
    {
        public List<string> CbCreateAppendItems { get; set; }
        public string CbAppendCreate { get; set; }
        public DataSource()
        {
            CbCreateAppendItems = new List<string>() { "create", "append" };
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new DataSource();
        }
    }
}

コンボ ボックスで異なる値を選択すると、TextBlock が同じ値に更新されるため、VM のプロパティも更新されます。

于 2013-02-15T22:33:55.127 に答える