Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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# 在DataGridTemplateColumn中使用TemplateSelector时绑定不再工作_C#_Wpf_Xaml_Datatemplate_Datatemplateselector - Fatal编程技术网

C# 在DataGridTemplateColumn中使用TemplateSelector时绑定不再工作

C# 在DataGridTemplateColumn中使用TemplateSelector时绑定不再工作,c#,wpf,xaml,datatemplate,datatemplateselector,C#,Wpf,Xaml,Datatemplate,Datatemplateselector,我有以下(工作)XAML定义: <DataGridTemplateColumn Header="Station &amp; Programm" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ListBox

我有以下(工作)
XAML
定义:

<DataGridTemplateColumn Header="Station &amp; Programm" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>                                                
            <ListBox 
                ItemsSource="{Binding Targets}" 
                Style="{StaticResource ListBoxTransparentStyle}"    
                VerticalAlignment="Center"
                IsHitTestVisible="False"> <!--Disable Selection-->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="A"/>
                                <ColumnDefinition Width="5"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock
                                Grid.Column="0"
                                Text="{Binding Station}"
                                Height="Auto"/>
                            <TextBlock
                                Grid.Column="2"
                                Text="{Binding Program}"
                                Height="Auto"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
之后,我将代码更改如下:

<ControlTemplate>
    <ControlTemplate.Resources>
        <DataTemplate x:Key="StationProgramView" x:Shared="True">                                                
            <ListBox 
                ItemsSource="{Binding Targets}" 
                Style="{StaticResource ListBoxTransparentStyle}"    
                VerticalAlignment="Center"
                IsHitTestVisible="False"> <!--Disable Selection-->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid >
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="A"/>
                                <ColumnDefinition Width="5"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <controls:HtTextfeld
                                Grid.Column="0"
                                Text="{Binding Station}"
                                DisableTranslation="True"
                                Height="Auto"/>
                            <controls:HtTextfeld
                                Grid.Column="2"
                                Text="{Binding Program}"
                                DisableTranslation="True"
                                Height="Auto"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
        <DataTemplate x:Key="StationProgramEdit" x:Shared="True">                                                
            <TextBlock Text="Hello World"></TextBlock>
        </DataTemplate>
        <recipeControls:ZlsRouteEditorDataTemplateSelector x:Key="StationProgramTemplateSelector" ViewDataTemplate="{StaticResource StationProgramView}" EditDataTemplate="{StaticResource StationProgramEdit}"/>




已正确选择
DataTemplate
s,但数据绑定不再工作。

尝试设置列的
CellTemplateSelector
属性,而不是将
CellTemplate
设置为
ContentControl

<DataGridTemplateColumn Header="Station &amp; Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}" />


ContentControl
ContentTemplate
中根元素的
DataContext
是相同
ContentControl
内容

我已经更改了
ZlsRouteEditorDataTemplateSelector
类,该类能够查找我的特定控件(
ZlsRouteEditor
)并获取
模式

ZlsRouteEditorDataTemplateSelector
为什么要将DataTemplate的x:Shared属性设置为true?我想,如果您对多个实例(对于
DataGrid
中的每一行)使用
DataTemplate
,请设置此标志。感谢您的回复。我已经尝试过这个解决方案,但是如何将我的
Mode
属性(
Content=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=recipeControls:ZlsRouteEditor},Path=Mode}”/>
)传递到
StationProgramTemplateSelector
?根据其值,选择
数据模板
。-自己回答->在类中为其设置另一个
属性
!无论如何谢谢你!在类中为其设置另一个属性!不可能,因为无法绑定属性。绑定只能在
DependencyObject
s上进行,而
DataTemplateSelector
则不能。如果我使用解决方法来使用
ContentControl
(),我将遇到与我所问问题相同的问题!
<DataGridTemplateColumn Header="Station &amp; Programm" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ContentControl
                ContentTemplateSelector="{StaticResource StationProgramTemplateSelector}"
                Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=recipeControls:ZlsRouteEditor}, Path=Mode}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Station &amp; Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}" />
public class ZlsRouteEditorDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate ViewDataTemplate { get;set; }
    public DataTemplate EditDataTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is ERouteEditor e)
            return _GetTemplate(e);

        ZlsRouteEditor parent = container.TryFindParent<ZlsRouteEditor>();
        if (parent != null)
            return _GetTemplate(parent.Mode);

        return base.SelectTemplate(item, container);
    }

    private DataTemplate _GetTemplate(ERouteEditor e)
    {
        switch (e)
        {
            case ERouteEditor.View:
                return ViewDataTemplate;
            case ERouteEditor.Edit:
                return EditDataTemplate;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}
/// <summary>
/// Finds a parent of a given item on the visual tree.
/// </summary>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="child">A direct or indirect child of the
/// queried item.</param>
/// <returns>The first parent item that matches the submitted
/// type parameter. If not matching item can be found, a null
/// reference is being returned.</returns>
public static T TryFindParent<T>(this DependencyObject child) where T : DependencyObject
{
    //get parent item
    DependencyObject parentObject = GetParentObject(child);

    //we've reached the end of the tree
    if (parentObject == null) return null;

    //check if the parent matches the type we're looking for
    T parent = parentObject as T;
    if (parent != null)
    {
        return parent;
    }
    else
    {
        //use recursion to proceed with next level
        return TryFindParent<T>(parentObject);
    }
}
<DataGridTemplateColumn Header="Station &amp; Programm" Width="*" CellTemplateSelector="{StaticResource StationProgramTemplateSelector}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <recipeControls:ZlsRouteEditor Height="570" Margin="5" Mode="View"/>
    <recipeControls:ZlsRouteEditor Height="570" Margin="5" Mode="Edit"/>
</StackPanel>