编程语言
首页 > 编程语言> > c# – 使用DataTriggers在运行时更改DataTemplates

c# – 使用DataTriggers在运行时更改DataTemplates

作者:互联网

我正在尝试根据特定的布尔值更改WPF应用程序中的对象的DataTemplate.当值为“True”时,我希望DataTemplate成为某种东西,当值为“False”时,我希望DataTemplate成为其他东西.

我已经尝试编写这段代码,但到目前为止,我最终遇到了一个恼人的“Out of Memory异常”.

<DataTemplate DataType="{x:Type vm:MyObjectViewModel}">
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Visible}" Value="False">
            <Setter TargetName="MainTemplateGrid" Property="Content">
                <Setter.Value>
                    <Ellipse Width="50" Height="50" Fill="Red" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Visible}" Value="True">
            <Setter TargetName="MainTemplateGrid" Property="Content">
                <Setter.Value>
                    <Image Source="{Binding Icon}" Opacity="{Binding Visible, Converter={StaticResource VisibilityConverter}}" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </DataTemplate.Triggers>
    <ContentControl x:Name="MainTemplateGrid" />
</DataTemplate>

如果有人知道如何解决这个问题,请告诉我.

解决方法:

我想到了.

我使用ContentControl并使用DataTriggers设置其样式,这是代码:

 <DataTemplate DataType="{x:Type vm:MyControlViewModel}">
    <ContentControl>
        <ContentControl.ToolTip>
           <!-- TOOLTIP CODE GOES HERE -->
        </ContentControl.ToolTip>
        <ContentControl.InputBindings>
            <!-- INPUT BINDINGS CODE GOES HERE -->
        </ContentControl.InputBindings>
        <ContentControl.ContextMenu>
            <!-- CONTEXT MENU CODE GOES HERE -->
        </ContentControl.ContextMenu>
        <ContentControl.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="Visibility" Value="0">
                        <Setter Property="ContentControl.Content">
                            <Setter.Value>
                                <Ellipse Width="50" Height="50" Opacity="0.5">
                                    <Ellipse.Fill>
                                        <LinearGradientBrush>
                                            <GradientStopCollection>
                                                <GradientStop Color="Red" Offset="0" />
                                                <GradientStop Color="DarkRed" Offset="0.8" />
                                            </GradientStopCollection>
                                        </LinearGradientBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                    <DataTrigger Binding="Visibility" Value="100">
                        <Setter Property="ContentControl.Content">
                            <Setter.Value>
                                <Image Source="{Binding Icon}" Opacity="{Binding Visible, Converter={StaticResource VisibilityConverter}}" />
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    </ContentControl>
</DataTemplate>

标签:c,wpf,templating,datatemplate,datatrigger
来源: https://codeday.me/bug/20190630/1332870.html