WPFツールキットを使用してComboBoxをAutoCompleteBoxに変換する
「複雑な」ComboBoxを同様に複雑なAutoCompleteBoxに変換するのに少し問題があります。私の目標は、ShoppingCartのアイテムを選択して、リストのアイテムの1つと同じように設定できるようにすることです。これが私の状況を再現するために取るべき3つのステップです(私はStyletとそのSetAndNotify()INPCメソッドを使用しています):
2つのオブジェクトを作成します。1つはNameプロパティのみを持ち、もう1つはもう1つのオブジェクトのみをプロパティとして持ちます。
public class ItemModel : PropertyChangedBase { private string _name; public string Name { get => _name; set => SetAndNotify(ref _name, value); } } public class ShoppingCartModel : PropertyChangedBase { public ItemModel Item { get; set; } }
DataContextのItemsListとShoppingcartの両方を初期化して入力します(MVVMを使用しているため、ViewModelです)
public ShoppingCartModel ShoppingCart { get; set; } public ObservableCollection<ItemModel> ItemsList { get; set; } public ShellViewModel() { ItemsList = new ObservableCollection<ItemModel>() { new ItemModel { Name = "T-shirt"}, new ItemModel { Name = "Jean"}, new ItemModel { Name = "Boots"}, new ItemModel { Name = "Hat"}, new ItemModel { Name = "Jacket"}, }; ShoppingCart = new ShoppingCartModel() { Item = new ItemModel() }; }
AutoCompleteBox、ComboBox、およびビュー内の小さなTextBlockを作成して、すべてをテストします。
<Window [...] xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=DotNetProjects.Input.Toolkit"> <!-- Required Template to show the names of the Items in the ItemsList --> <Window.Resources> <DataTemplate x:Key="AutoCompleteBoxItemTemplate"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Background="Transparent"> <Label Content="{Binding Name}"/> </StackPanel> </DataTemplate> </Window.Resources> <StackPanel> <!-- AutoCompleteBox: can see the items list but selecting doesn't change ShoppingCart.Item.Name --> <Label Content="AutoCompleteBox with ShoppingCart.Item.Name as SelectedItem:"/> <toolkit:AutoCompleteBox ItemsSource="{Binding ItemsList}" ValueMemberPath="Name" SelectedItem="{Binding Path=ShoppingCart.Item.Name}" ItemTemplate="{StaticResource AutoCompleteBoxItemTemplate}"/> <!-- ComboBox: can see the items list and selecting changes ShoppingCart.Item.Name value --> <Label Content="ComboBox with ShoppingCart.Item.Name as SelectedValue:"/> <ComboBox ItemsSource="{Binding ItemsList}" DisplayMemberPath="Name" SelectedValue="{Binding Path=ShoppingCart.Item.Name}" SelectedValuePath="Name" SelectedIndex="{Binding Path=ShoppingCart.Item}" /> <!-- TextBox: Typing "Jean" or "Jacket" updates the ComboBox, but not the AutoCompleteBox --> <Label Content="Value of ShoppingCart.Item.Name:"/> <TextBox Text="{Binding Path=ShoppingCart.Item.Name}"/> </StackPanel> </window>
双方向にAutoCompleteBoxさんのSelectedItemの結合モードを変更すると、「それは印刷します[プロジェクト名] .ItemModel」どの手段(私は推測?)それはItemModelsではなく、文字列を取得していますが、私はそれを動作させるように見えることはできません。どんな助けでもありがたいです、感謝し、それを小さくすることが可能であるならば私の投稿を編集してください。
回答
何度も試みた後、私はついに犯人を見つけました:
継承に
ShoppingCartModel.Item
もかかわらずINPCが実装されていない(INPCを実装するか、継承作業を削除する)PropertyChangedBase
PropertyChangedBase
public class ShoppingCartModel : PropertyChangedBase { private ItemModel _item; public ItemModel Item { get => _item; set => SetAndNotify(ref _item, value); } }
AutoCompleteBox
SelectedItem
は同じタイプでItemsSource
あり、TwoWay
モードを持っている必要がありますBinding
<toolkit:AutoCompleteBox ItemsSource="{Binding ItemsList}" ValueMemberPath="Name" SelectedItem="{Binding Path=ShoppingCart.Item, Mode=TwoWay}" ItemTemplate="{StaticResource AutoCompleteBoxItemTemplate}"/>
そして最後に...最も不思議なのはComboBoxです!そこにいるだけでそれは混乱し、
AutoCompleteBox
理由はわかりません。ComboBox全体にコメントするだけですべてが機能します。ComboBoxがAutoCompleteBoxのバインディングを壊す理由を知っている場合は、遠慮なく助けてください。
ListView内でAutoCompleteBoxを使用する場合は別の問題がありますが、ここでその問題について別の質問を作成することをお勧めします