gpt4 book ai didi

c# - 条件 XAML (WPF)

转载 作者:可可西里 更新时间:2023-11-01 08:36:13 24 4
gpt4 key购买 nike

我正在尝试创建一个用户控件,根据用户在依赖属性中设置的模式,将用户控件更改为一个 TextBlock 和另一个 TextBlock,或者一个 TextBlock 和一个 TextBox。我知道依赖属性正在获取信息,但是当我尝试设置正确的模板时出现了问题。由于某种原因,模板无法正确呈现。

XAML:

<UserControl
x:Class="BookOrganizer.FlipBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:BookOrganizer"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Horizontal" Height="Auto" Width="Auto" >
<StackPanel.Resources>
<ContentControl x:Key="Box">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" Height="Auto" Width="Auto" />
<TextBox Text="{Binding Path=Text}" Height="Auto" Width="Auto" />
</StackPanel>
</ContentControl>
<ContentControl x:Key="Block" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal" Height="Auto" Width="Auto">
<TextBlock Text="{Binding Path=Title}" Height="Auto" Width="Auto" />
<TextBlock Text="{Binding Path=Text}" Height="Auto" Width="Auto"/>
</StackPanel>
</ContentControl>
</StackPanel.Resources>
<ContentControl Template="{Binding Path=BoxMode}" />
</StackPanel>
</UserControl>

代码隐藏:

using System;
using System.Windows;
using System.Windows.Controls;

namespace BookOrganizer
{
/// <summary>
/// Interaction logic for FlipBox.xaml
/// </summary>
public partial class FlipBox : UserControl
{
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
"Title", typeof(String), typeof(FlipBox), new PropertyMetadata("nothing"));

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(String), typeof(FlipBox), new PropertyMetadata("nothing"));

public static readonly DependencyProperty BoxModeProperty = DependencyProperty.Register(
"BoxMode", typeof(String), typeof(FlipBox), new PropertyMetadata("Box"));

public FlipBox()
{
InitializeComponent();
this.DataContext = this;
}

public String Title
{
get { return (String)this.GetValue(TitleProperty); }
set { this.SetValue(TitleProperty, value); }
}

public String Text
{
get { return (String)this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}

public String BoxMode
{
get { return (String)this.GetValue(BoxModeProperty); }
set { this.SetValue(BoxModeProperty, value); }
}

}
}

提前致谢。

最佳答案

您可以使用触发器来设置模板。将您的 UserControl 中的 StackPanel 替换为...

   <StackPanel Orientation="Horizontal" Height="Auto" Width="Auto" >
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=BoxMode}" Value="Box">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal" Height="Auto" Width="Auto">
<TextBlock Text="{Binding Path=Title}" Height="Auto" Width="Auto" />
<TextBlock Text="{Binding Path=Text}" Height="Auto" Width="Auto"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=BoxMode}" Value="Block">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Title}" Height="Auto" Width="Auto" />
<TextBox Text="{Binding Path=Text}" Height="Auto" Width="Auto" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</StackPanel>

关于c# - 条件 XAML (WPF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4875739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com