开发者

BitmapFrame in WPF ComboBox

开发者 https://www.devze.com 2022-12-08 11:34 出处:网络
I have a dialog that lets the user pick a custom icon. Loaded icons are displayed in a combobox in which the user makes the selection. Here is the XAML code for the combobox:

I have a dialog that lets the user pick a custom icon. Loaded icons are displayed in a combobox in which the user makes the selection. Here is the XAML code for the combobox:

<ComboBox 
    Style="{DynamicResource IconComboBox}"
    ItemTemplate="{StaticResource IconTemplate}"
    ItemsSource="{Binding 开发者_如何学运维Icons,
        RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type UserControl}}}"
    SelectedItem="{Binding Icon}"
    />
  • Icons is a property that returns an array of BitmapFrame.
  • Icon is a property for the currently selected.

What currently doesnt work is that when I through the code behind set the Icon property (for example, when displaying which Icon the user picked the last time the dialog was shown), the Combobox selection is just blank. As if I had set the property to null before calling OnPropertyChanged.

My current theory is that WPF cannot compare BitmapFrames, so when walking through the list it fails to determine any of the icons there the "equal" and thus assumes null.

In short; what is the best way to through code behind set the currently selected item, which is a BitmapFrame, in a combobox?


You can use SelectedItem or SelectedIndex in the code behind to set the currently selected item!

0

精彩评论

暂无评论...
验证码 换一张
取 消