开发者

Weird databinding issues in wpf combobox

开发者 https://www.devze.com 2023-02-14 17:32 出处:网络
I am writing wh开发者_开发问答at is turning out to be a simple GUI in WPF.At the moment I have a static list inside of a ComboBox, like this:

I am writing wh开发者_开发问答at is turning out to be a simple GUI in WPF. At the moment I have a static list inside of a ComboBox, like this:

    <ComboBox HorizontalAlignment="Left" Height="22" Margin="24,97,0,0" VerticalAlignment="Top" Width="83"
        SelectedItem="{Binding fruit, Mode=TwoWay}">
        <ComboBoxItem>apple</ComboBoxItem>
        <ComboBoxItem>orange</ComboBoxItem>
        <ComboBoxItem>grape</ComboBoxItem>
        <ComboBoxItem>banana</ComboBoxItem>
    </ComboBox>

I'm binding the SelectedItem to a singleton in my code that has already been initialized and used elsewhere.

I put a breakpoint on the get of fruit and it returns "grape", but the selected item is always blank. I even added a button so that I could call RaisePropertyChanged manually, but the RaisePropertyChange call didn't do anything either.

Finally, MVVMLight gives blendability. For no important reason I changed the binding in the combobox from SelectedItem to Text As soon as I did that, my design time form filled in with the expected values, but, when the code is running, the box continues to sit at the empty state


This is because you have items of type ComboBoxItem in the ComboBox but the property you are trying to bind to is of type string.

You have three options:

1.Instead of adding ComboBoxItem items add String items:

<ComboBox HorizontalAlignment="Left" Height="22" Margin="24,97,0,0" VerticalAlignment="Top" Width="83"
    SelectedItem="{Binding fruit, Mode=TwoWay}">
    <sys:String>apple</sys:String>
    <sys:String>orange</sys:String>
    <sys:String>grape</sys:String>
    <sys:String>banana</sys:String>
</ComboBox>

2.Instead of SelectedItem bind to SelectedValue and specify SelectedValuePath as Content:

<ComboBox HorizontalAlignment="Left" Height="22" Margin="24,97,0,0" VerticalAlignment="Top" Width="83"
    SelectedValue="{Binding fruit, Mode=TwoWay}"
    SelectedValuePath="Content">
    <ComboBoxItem>apple</ComboBoxItem>
    <ComboBoxItem>orange</ComboBoxItem>
    <ComboBoxItem>grape</ComboBoxItem>
    <ComboBoxItem>banana</ComboBoxItem>
</ComboBox>

3.Do not specify items directly in XAML, but use ItemsSource property to bind to a collection of strings:

<ComboBox HorizontalAlignment="Left" Height="22" Margin="24,97,0,0" VerticalAlignment="Top" Width="83"
    ItemsSource="{Binding Fruits}"
    SelectedItem="{Binding fruit, Mode=TwoWay}"/>


You should bind ComboBox.ItemSource to a list of strings (make the List of strings an ObservableCollection<string> in case you add items to this list) and then set the fruit variable to an instance in the List of strings.

I think you have your problem because your fruit variable references a different instance than you have in your list of ComboBoxItems. (even though the strings are the same)

0

精彩评论

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