开发者

WPF Combobox is changing source via SelectedValue when ItemsSource is loaded

开发者 https://www.devze.com 2023-03-20 06:25 出处:网络
I have this combobox in my WPF window. <ComboBox DisplayMemberPath=\"Description\" SelectedValuePath=\"ID\" ItemsSource=\"{Binding Source={StaticResource CvsPrinters}}\" SelectedValue=\"{Binding C

I have this combobox in my WPF window.

<ComboBox DisplayMemberPath="Description" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource CvsPrinters}}" SelectedValue="{Binding CheckPrinterID}" />

My problem is that when loading the window, the SelectedValue binding is causin开发者_如何学JAVAg my source data to change to the first item in the ItemsSource, instead of setting the Combobox's SelectedValue to the appropriate item in the ItemsSource.

The CheckPrinterID is from a listview selection datacontext, and this problem only occurs to the item initially selected in that listview on load. When I select another item in the listbox, the combobox correctly selects the proper item and all is fine, but unfortunately my initial item has been updated and is now incorrect.


I guess you are trying to synchronize ListView and ComboBox through a common property. Try setting IsSynchronizedWithCurrentItem to True in ListView and make sure SelectedItem or SelectedIndex for ListView is set during load.


Try re-arranging ItemsSource before DisplayMemberPath.


If you have some flexibility in the DataContext object you could try changing the selected CheckPrinter property to be of the data object type instead of the ID and switch to using SelectedItem instead of SelectedValue (for some reason SelectedValue behaves differently, especially at initial load) and then extract the ID from that value in code.

If you can't use the CheckPrinter objects in your DataContext object for whatever reason, you could also go the opposite direction on the UI side by using a list of IDs as the ItemsSource, and again using SelectedItem. To get the list to show what you want in the ComboBoxItems you would then need to use an IValueConverter to pull out Description values based on IDs:

<ComboBox ItemsSource="{Binding Source={StaticResource CvsPrinterIds}}" SelectedItem="{Binding CheckPrinterID}" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock >
                <TextBlock.Text>
                    <Binding>
                        <Binding.Converter>
                            <local:MyDescriptionLookupConverter Printers="{StaticResource CvsPrinters}"/>
                        </Binding.Converter>
                    </Binding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

and a simple converter to do the ID-Description lookup (add some null and cast checks):

    public class MyDescriptionLookupConverter : IValueConverter
    {
        public IEnumerable<Printer> Printers { get; set; }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Printers.First(p => p.Id == (int)value).Description;
        }
        ...
    }
0

精彩评论

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

关注公众号