我正在尝试创建一个用户控件,根据用户在依赖项属性中设置的模式,将用户控件更改为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>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using System;
using System.Windows;
using System.Windows.Controls;
namespace BookOrganizer
{
/// <summary>
/// Interaction logic for FlipBox.xaml …Run Code Online (Sandbox Code Playgroud) 所以我创建了这个类Sprite.cs:
class Sprite : INotifyPropertyChanged
{
double _Speed;
RectangleGeometry _SpriteRectangleGeometry;
Path _SpritePath;
public Sprite()
{
_SpriteRectangleGeometry = new RectangleGeometry();
_SpriteRectangleGeometry.Rect = new Rect(0, 0, 50, 50);
Speed = 50;
_SpritePath = new Path();
Color = Brushes.Black;
_SpritePath.Data = _SpriteRectangleGeometry;
}
public Sprite(double xpos, double ypos, double height, double width, double speed, SolidColorBrush color)
{
_SpriteRectangleGeometry = new RectangleGeometry();
_SpriteRectangleGeometry.Rect = new Rect(xpos, ypos, width, height);
this.Speed = speed;
_SpritePath = new Path();
this.Color = color;
_SpritePath.Data = _SpriteRectangleGeometry;
}
public …Run Code Online (Sandbox Code Playgroud)