0

次のコードで、プロパティIsSelectedが設定されたアイテムが、をクリックした後と同じようtrueに選択されないのはなぜですか? ComboBoxListBoxButtonここに画像の説明を入力

をクリックするComboBoxと、選択したアイテムが選択されますが、前ではありません。 ここに画像の説明を入力

xaml:

<Window x:Class="WpfApplication1.Desktop.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="350" Width="525">
    <StackPanel>
        <ListBox ItemsSource="{Binding Items}">
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsSelected" 
                            Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                </Style>
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Txt}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <ComboBox ItemsSource="{Binding Items}">
            <ComboBox.Resources>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="IsSelected" 
                            Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                </Style>
            </ComboBox.Resources>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Txt}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <Button Content="Select second item" Click="Button_Click"  />
    </StackPanel>
</Window>

xaml.cs:

using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Windows;
using Microsoft.Practices.Prism.ViewModel;

namespace WpfApplication1.Desktop
{
    [Export]
    public partial class Shell : Window
    {
        public class Foo : NotificationObject
        {
            static int _seq = 0;
            string _txt = "Item " + (++_seq).ToString();
            public string Txt { get { return _txt; } }
            bool _isSelected;
            public bool IsSelected
            {
                get { return _isSelected; }
                set
                {
                    _isSelected = value; 
                    RaisePropertyChanged(() => IsSelected);
                }
            }
        }

        public ObservableCollection<Foo> Items { get; set; }

        public Shell()
        {
            Items = new ObservableCollection<Foo>();
            for (int i = 0; i < 5; i++)
                Items.Add(new Foo());
            DataContext = this;
            InitializeComponent();
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            Items[1].IsSelected = true;
        }
    }
}
4

3 に答える 3

3

これは、ItemContainerStyle が ComboBoxItems が生成されたとき (つまり、ドロップダウンを開いたとき) にのみ適用されるためです。

これを回避するには、SelectedItem という別のプロパティを作成し、コンボボックスの SelectedValue をそれにバインドします。

長い説明と例はこちら

于 2012-05-15T21:22:12.567 に答える
1

バインドはUpdateSourceTrigger=LostFocusデフォルトでオンに設定されているため、目的の結果を得るには、バインドを変更する必要がありPropertyChangedます。このような:

<Style TargetType="ListBoxItem">
  <Setter Property="IsSelected"Value="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</Style>
于 2012-05-15T20:47:06.503 に答える
0

モデルをDataContextWPF ウィンドウの として使用する場合、コントロールは最初は期待どおりに動作しない場合があります。基本的に、一部のプロパティ/イベントは、ウィンドウが初期化されるまで設定/呼び出されません。この場合の回避策は、ウィンドウのLoadedイベントでバインドをセットアップすることです。

免責事項:OPの特定のシナリオでこれをテストしていませんが、これは私が過去に遭遇した動作と回避策です。

于 2012-05-15T20:46:56.510 に答える