WPF DataTemplate选择器无法正常工作

时间:2015-03-05 19:18:02

标签: c# wpf itemscontrol hierarchicaldatatemplate datatemplateselector

我正在开发Treeview控件。 items控件应根据数据结构的值动态显示一组textbox和combobox。 执行ArgumentTypeTemplateSelector的转换代码。但是,不显示文本框和组合。请有人帮忙。谢谢。

树视图(xaml)

                  <ItemsControl x:Name="argumentTexts" ItemsSource="{Binding ArgumentDetailsCollection}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="{x:Type structures:ArgumentDetails}">
                                <ItemsControl x:Name="items" ItemsSource="{Binding DefaultValue}" 
                                              ItemTemplateSelector="{Binding DefaultValue, Converter={StaticResource ArgTypeTemplateSelector}}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>


public class ArgumentTypeTemplateSelector : IValueConverter
{
    private DataTemplate comboboxDataTemplate;

    private DataTemplate textboxDataTemplate;

    public DataTemplate ComboBoxDataTemplate
    {
        get
        {
            return this.comboboxDataTemplate;
        }

        set
        {
            this.comboboxDataTemplate = value;
        }
    }

    public DataTemplate TextBoxDataTemplate
    {
        get
        {
            return this.textboxDataTemplate;
        }

        set
        {
            this.textboxDataTemplate = value;
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string str = (string)value;

        if (str.Contains("1"))
        {
            return this.ComboBoxDataTemplate;
        }

        return this.TextBoxDataTemplate;
    }

在resourcedictionary(xaml)

        <DataTemplate x:Key="TextBoxDataTemplate">
            <TextBox Text="{Binding DefaultValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                         VerticalAlignment="Center" 
                                         Width="Auto" 
                                         Margin="5,0,0,0"
                                         Padding="0" 
                                         Style="{StaticResource GridEditStyle}"
                                         IsEnabled="True"/>
        </DataTemplate>
        <DataTemplate x:Key="ComboBoxDataTemplate">
            <ComboBox HorizontalAlignment="Stretch" IsEnabled="True"/> 
        </DataTemplate>
        <columnConfiguratorControls:ArgumentTypeTemplateSelector x:Key="ArgTypeTemplateSelector" ComboBoxDataTemplate="{StaticResource ComboBoxDataTemplate}" TextBoxDataTemplate="{StaticResource TextBoxDataTemplate}"/>
    </ResourceDictionary>

1 个答案:

答案 0 :(得分:2)

DataTemplateSelectors的工作方式与转换器不同。您的DataTemplateSelector将检索列表中的项目作为参数,并根据您定义的条件,您可以选择应返回的DataTemplate。

尝试以下方法: 在您的xaml文件中,将DataTemplateSelector定义为静态资源,并将其设置在ItemsControl中:

<ItemsControl x:Name="argumentTexts" ItemsSource="{Binding ArgumentDetailsCollection}">
        <ItemsControl.Resources>
            <!-- define your template selector as static resource to reference it later -->
            <ns:ArgumentTypeTemplateSelector x:Key="ArgumentTypeTemplateSelector"/>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type structures:ArgumentDetails}">
                <!-- use your template selector here -->
                <ItemsControl x:Name="items" ItemsSource="{Binding DefaultValue}" 
                              ItemTemplateSelector="{StaticResource ArgumentTypeTemplateSelector }"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

在代码隐藏文件中,您需要以下列方式实现数据模板选择器:

// derive from base class DataTemplateSelector
public class ArgumentTypeTemplateSelector : DataTemplateSelector
{
    // override SelectTemplate method
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        // get access to the resources you need, for example
        // by accessing the UI object where your selector is placed in
        var frameworkElement = (FrameworkElement) container;

        // return a data template depending on your custom logic,
        // you can cast "item" here to the specific type and check your conditions
        if(item has condition)
        return (DataTemplate) frameworkElement.FindResource("YourDataTemplateKey");
        else
            // ...
        return base.SelectTemplate(item, container);
    }
}