WPF数据网格中的ComboBox DataTemplate绑定未初始化

时间:2015-03-05 09:04:01

标签: wpf binding combobox datatemplate

我有一个模型和DataGrid的绑定列表。值可以是几种数据类型(bool,double)。

    public class Model
    {
        public object Value { get; set; }
    }

public void Initialize()
{
            var models = new BindingList<Model>();
            models.Add(new Model(){ Value = "hello"});
            models.Add(new Model(){Value=true});
            signals.ItemsSource = models;
}

我想在数据网格中显示数据,我想使用文本框表示数字,但是对于布尔值使用combo(true / false)。所以我实现了bool2string转换器和DataTemplateSelector。在我的示例中,我有一个文本列和一个显示相同数据的模板列。当我启动应用程序时,未初始化组合值(未选择任何内容)。一旦我开始玩值,一切正常,值正确同步(如果我在一列中更改值,它将传播到另一列)。你知道吗,我做了什么错误?

enter image description here

public class BoolToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((bool)value == true) ? "true" : "false";
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var comboValue = (value as ComboBoxItem).Content.ToString();
        return (String.Compare(comboValue, "true", StringComparison.InvariantCultureIgnoreCase) == 0);
    }
}

public class DynamicDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var element = container as FrameworkElement;
        var signal = item as Model;
        if (element != null && signal != null)
        {
            if (signal.Value is bool)
                return element.FindResource("BoolTemplate") as DataTemplate;
            else
                return element.FindResource("TextTemplate") as DataTemplate;
        }
        return null;
    }
}

我的xaml如下所示:

<Window.Resources>
    <comboQuestion:DynamicDataTemplateSelector x:Key="DataTemplateSelector"/>
    <comboQuestion:BoolToStringConverter x:Key="BoolToStringConverter"/>
</Window.Resources>
<Grid>
    <DataGrid Grid.Row="1"  Name="signals" AutoGenerateColumns="False"  ItemsSource="{Binding}" >
        <DataGrid.Resources>
            <DataTemplate x:Key="BoolTemplate">
                <ComboBox SelectedItem="{Binding Value, Converter={StaticResource BoolToStringConverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                    <ComboBoxItem>true</ComboBoxItem>
                    <ComboBoxItem>false</ComboBoxItem>
                </ComboBox>
            </DataTemplate>
            <DataTemplate x:Key="TextTemplate">
                <TextBox Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="TextValue" Binding="{Binding Value}"/>
            <DataGridTemplateColumn Header="DynamicValue" CellTemplateSelector="{StaticResource DataTemplateSelector}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

编辑: 我试图将SelectedItem更改为SelectedValue,我也尝试将转换器的转换部分更改为:

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var cbi = new ComboBoxItem();
        cbi.Content = ((bool)value == true) ? "true" : "false";
        return cbi;
    }
然而,行为仍然是一样的。

1 个答案:

答案 0 :(得分:1)

您可以使用以下转换器将布尔值转换为索引号:

public class BooleanToIndexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((bool)value == true) ? 0 : 1;   
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return ((int)value == 0) ? true : false;
        }
    }

然后绑定到ComboBox上的 SelectedIndex

SelectedIndex="{Binding Value, Converter={StaticResource BooleanToIndexConverter}}