DataTemplate中具有空值的ComboBox

时间:2015-06-24 10:53:24

标签: c# wpf datatemplate

我在使用DataTemplate中的ComboBox时遇到空值问题。

第一个screendump显示了两个带有DataTemplate的组合框。

此第一个选定值为空白。最后选择的值是正确的。

enter image description here

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate DataType="{x:Type local:PropertyValueViewModel}" >
        <ComboBox Margin="9" SelectedValue="{Binding Value}" ItemsSource="{Binding SelectableValues}" DisplayMemberPath="Description" SelectedValuePath="Value" />
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ContentControl Content="{Binding ValueSelector, UpdateSourceTrigger=PropertyChanged}"></ContentControl>
    <ContentControl Content="{Binding ValueSelector, UpdateSourceTrigger=PropertyChanged}"></ContentControl>
</StackPanel>

如果我使用没有数据模板的组合框,则两个组合框都显示正确的值。

enter image description here

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Test"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <ComboBox SelectedValue="{Binding ValueSelector.Value}" ItemsSource="{Binding ValueSelector.SelectableValues}" DisplayMemberPath="Description" SelectedValuePath="Value"/>
    <ComboBox SelectedValue="{Binding ValueSelector.Value}" ItemsSource="{Binding ValueSelector.SelectableValues}" DisplayMemberPath="Description" SelectedValuePath="Value"/>
</StackPanel>

代码背后:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ValueSelector = new PropertyValueViewModel()
        {
            SelectableValues = new List<SelectableValue>()
            {
                new SelectableValue("NULL", null),
                new SelectableValue("1", 1)
            },
            Value = null
        };

        DataContext = this;

    }

    public static readonly DependencyProperty ValueSelectorProperty = DependencyProperty.Register(
        "ValueSelector", typeof(PropertyValueViewModel), typeof(MainWindow), new PropertyMetadata(default(PropertyValueViewModel)));

    public PropertyValueViewModel ValueSelector
    {
        get { return (PropertyValueViewModel)GetValue(ValueSelectorProperty); }
        set { SetValue(ValueSelectorProperty, value); }
    }
}

/// <summary>
/// My viewModel
/// </summary>
public class PropertyValueViewModel
{
    public object Value { get; set; }
    public object SelectableValues { get; set; }
}

/// <summary>
/// The items in the combobox
/// </summary>
public class SelectableValue
{
    public SelectableValue(string header, object value)
    {
        Value = value;
        Description = header;
    }

    public object Value { get; set; }

    public string Description { get; set; }
}

为什么ComboBox会改变空值的行为?

我在这里创建了一个演示示例:http://1drv.ms/1fAkP1d

0 个答案:

没有答案
相关问题