Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF数据模板选择器不工作_C#_Wpf_Itemscontrol_Hierarchicaldatatemplate_Datatemplateselector - Fatal编程技术网

C# WPF数据模板选择器不工作

C# WPF数据模板选择器不工作,c#,wpf,itemscontrol,hierarchicaldatatemplate,datatemplateselector,C#,Wpf,Itemscontrol,Hierarchicaldatatemplate,Datatemplateselector,我正在使用Treeview控件。items控件应该根据数据结构的值动态显示一组textbox和combobox。 将执行ArgumentTypeTemplateSelector的转换代码。但是,不显示文本框和组合框。请找个人帮忙。多谢各位 树视图(xaml) 公共类ArgumentTypeTemplateSelector:IValueConverter { 私有数据模板ComboxDataTemplate; 私有数据模板textboxDataTemplate; 公共数据模板ComboxData

我正在使用Treeview控件。items控件应该根据数据结构的值动态显示一组textbox和combobox。 将执行ArgumentTypeTemplateSelector的转换代码。但是,不显示文本框和组合框。请找个人帮忙。多谢各位

树视图(xaml)


公共类ArgumentTypeTemplateSelector:IValueConverter
{
私有数据模板ComboxDataTemplate;
私有数据模板textboxDataTemplate;
公共数据模板ComboxDataTemplate
{
得到
{
返回此.comboboxDataTemplate;
}
设置
{
this.comboboxDataTemplate=值;
}
}
公共数据模板TextBoxDataTemplate
{
得到
{
返回此.textboxDataTemplate;
}
设置
{
this.textboxDataTemplate=值;
}
}
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
字符串str=(字符串)值;
如果(str.Contains(“1”))
{
返回此.ComboBoxDataTemplate;
}
返回此.TextBoxDataTemplate;
}
在resourcedictionary(xaml)中


DataTemplateSelector的工作方式与转换器不同。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>

您的模板选择器应该实现
DataTemplateSelector
或类似的东西,而不是
IValueConverter
,这是用于转换器的。检查此项您以错误的方式使用了
ItemTemplateSelector
依赖项属性。可能您根本不需要任何
ItemTemplateSelector
。您想要的标准是什么ode>DataTemplate基于?Dymanoid的选择:这用于用户输入,当用户在树状视图控件中选择项时发生。如果数据结构包含某些枚举类型,我希望将树状视图项中的文本块更改为包含枚举列表的组合框。如果数据结构包含类型double或int,我将uld喜欢显示文本框。数据结构可以包含枚举和双精度列表。我应该使用DataTemplate选择来完成这项工作吗?谢谢。谢谢。我会尝试。Elinigo:
        <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>
<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);
    }
}