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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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# 如何在运行时更改数据模板?_C#_Wpf_Xaml_Datagrid_Datatemplate - Fatal编程技术网

C# 如何在运行时更改数据模板?

C# 如何在运行时更改数据模板?,c#,wpf,xaml,datagrid,datatemplate,C#,Wpf,Xaml,Datagrid,Datatemplate,在我的应用程序中,我有一个复选框,我希望当它被选中时,显示一个DataTemplate,当它被取消选中时,显示另一个 下面是包含两个模板的代码段 <DataGrid x:Name="dataGrid" LoadingRow="dataGrid_LoadingRow_1" ItemsSource="{Binding Item3}" <DataGrid.RowHeaderTemplate> <DataTemplate>

在我的应用程序中,我有一个
复选框
,我希望当它被选中时,显示一个
DataTemplate
,当它被取消选中时,显示另一个

下面是包含两个模板的代码段

    <DataGrid x:Name="dataGrid" LoadingRow="dataGrid_LoadingRow_1" ItemsSource="{Binding Item3}"
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StudentId}"/>
            </DataTemplate>                
            <DataTemplate>
                <TextBlock Text="{Binding FullName}"/>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
    </DataGrid>

您只能通过触发器来完成。如果以上是你的要求。您只需通过触发器即可完成。我试过了,它对我有效

   <Window.Resources>
    <ControlTemplate x:Key="MyRowHeaderTemplate">
        <TextBlock x:Name="RowHeaderTxt" 
                   Text="{Binding StudentId, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsChecked, ElementName=MyCheckBox}" 
                         Value="True">
                <Setter TargetName="RowHeaderTxt" Property="Text" 
                        Value="{Binding FullName, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<StackPanel>
    <CheckBox x:Name="MyCheckBox"/>
    <DataGrid ItemsSource="{Binding Item3}" AutoGenerateColumns="True">
        <DataGrid.RowHeaderTemplate>
            <DataTemplate>
                <ContentControl Template="{StaticResource MyRowHeaderTemplate}"/>
            </DataTemplate>
        </DataGrid.RowHeaderTemplate>
    </DataGrid>
</StackPanel>


试试这个。

Lookup
DataTemplateSelector
,我认为它应该很好地满足您的需要。好的,这不是我真正的怀疑,我的意思是,当复选框状态更改时,我希望它可以隐藏,启动数据模板Selector或查看。好的,一个选项是创建一个从
ContentControl
派生的类,比如说有三个依赖属性:两个类型为
DataTemplate
,用于模板,一个类型为
bool
,用于绑定到
CheckBox.IsChecked
。然后,此控件可以根据
Boolean
-属性的值设置适当的模板,并在更改依赖项属性时更新模板。然后将该类的一个瞬间放入
RowHeaderTemplate
DataTemplate
,并添加适当的绑定。