0

コンボボックスのあるウィンドウがあります。このコンボボックスには 5 つの ComboboxItems があります。

コード ビハインド ファイルで、SelectedItem (コンボボックス) を ComboBoxSelectedIndex プロパティにバインドします。

この例では、項目 4 と 5 を選択できないようにします。

しかし、項目 4 と 5 を選択できます。何が問題なのですか?

xaml コード:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStartupLocation="CenterScreen"
        Height="350"
        Width="500">
    <StackPanel VerticalAlignment="Center">
        <ComboBox SelectedIndex="{Binding Path=ComboBoxSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem>Item 1</ComboBoxItem>
            <ComboBoxItem>Item 2</ComboBoxItem>
            <ComboBoxItem>Item 3</ComboBoxItem>
            <ComboBoxItem>Item 4</ComboBoxItem>
            <ComboBoxItem>Item 5</ComboBoxItem>
        </ComboBox>     
    </StackPanel>
</Window>

コードビハインド ファイル:

namespace WpfApplication1
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        private int _selectedIndex;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        public int ComboBoxSelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                if (value < 3)
                {
                    _selectedIndex = value;
                }
                OnPropertyChanged("ComboBoxSelectedIndex");
                Trace.WriteLine(ComboBoxSelectedIndex);
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}

(プロパティ Is Enabled を使用してこの問題を解決できることは承知していますが、ここでは説明しません)

4

3 に答える 3

0

何が起こるか:

  • 項目を選択します。
  • バインディングは MainWindow.SelectedIndex.set() を呼び出します
  • OnPropertyChanged はプロパティの変更を示します... しかし、WPF は既にこのプロパティを設定しているため、情報は破棄されます。

これは WPF バインディングの非常に厄介な問題です... できることは、WPF がコンボボックスでプロパティの設定を完了するのを待ってから、NotifyPropertyChange をトリガーすることです。これは、セッターで新しいスレッドを作成して、ディスパッチャー スレッドで通知を返すことができます。

私はこの問題を何度も抱えており、SelectedItem と SelectedIndex の両方のバインドを完了しました...両方の間でしばしば失われます:(

于 2012-08-10T07:13:52.987 に答える
0

これを実装するカスタム コンボボックスを作成します。

<StackPanel Orientation="Vertical">
    <wpfProj:ExtendedCombobBox 
       SelectedIndex="{Binding Path=ComboBoxSelectedIndex, 
                              Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       MaxSelectedIndex="{Binding Path=MaxSelectedIndex}">
        <ComboBoxItem>Item 1</ComboBoxItem>
        <ComboBoxItem>Item 2</ComboBoxItem>
        <ComboBoxItem>Item 3</ComboBoxItem>
        <ComboBoxItem>Item 4</ComboBoxItem>
        <ComboBoxItem>Item 5</ComboBoxItem>
    </wpfProj:ExtendedCombobBox>
</StackPanel>

そして、カスタムコンボボックスをコーディングします

public class ExtendedCombobBox:ComboBox
{
    public static readonly DependencyProperty MaxSelectedIndexProperty =
        DependencyProperty.Register("MaxSelectedIndex", typeof (int), typeof (ExtendedCombobBox), new PropertyMetadata(default(int)));

    public int MaxSelectedIndex
    {
        get { return (int) GetValue(MaxSelectedIndexProperty); }
        set { SetValue(MaxSelectedIndexProperty, value); }
    }

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        if (Items.IndexOf(e.AddedItems[0]) > MaxSelectedIndex)
            e.Handled = true;
        else
            base.OnSelectionChanged(e);
    }
}

UPD1. または、標準のものを使用して、SelectionChanged イベントをサブスクライブすることもできます。しかし、カスタムボボックスを使用したいと思います。

于 2012-08-10T07:37:55.053 に答える
0

戻り値は int32 ではなく文字列です。

public string ComboBoxSelectedIndex
    {
        get { return _selectedIndex; }
        set
        {
            if (int.parse(value) < 3)
            {
                _selectedIndex = value;
            }
            OnPropertyChanged("ComboBoxSelectedIndex");
            Trace.WriteLine(ComboBoxSelectedIndex);
        }
    }
于 2016-03-18T14:58:41.627 に答える