我有一个“RoomType”类,我正在创建一个可以将 RoomType 作为属性并显示名称的用户控件。我正在尝试将用户控件与属性绑定,如下所示:
public static readonly DependencyProperty TypeProperty =
DependencyProperty.Register("Type", typeof(RoomType), typeof(RoomTypeBox));
public RoomType Type
{
get
{
return (RoomType)GetValue(TypeProperty);
}
set
{
SetValue(TypeProperty, value);
}
}
问题是,当我将值(一个 RoomType 对象)设置为属性时,它会抛出“StackOverFlow 异常”,我已经逐步调试并且不存在无限循环。
这是 UserControl 的 XAML
<UserControl x:Class="NH_Motel.Components.RoomTypeBox"
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"
mc:Ignorable="d" Width="310.667" Height="28">
<Grid Name="Base" Height="28">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180*" />
<ColumnDefinition Width="0*" />
</Grid.ColumnDefinitions>
<Button Content="..." HorizontalAlignment="Right" x:Name="btnSelect" Width="47" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment" Height="28" Style="{DynamicResource ButtonStyle3}" Click="btnSelect_Click" />
<TextBox x:Name="txbText" Height="28" Margin="0,0,51,0" TextWrapping="Wrap" VerticalAlignment="Top" Template="{DynamicResource TextBoxControlTemplate1}" d:LayoutOverrides="HorizontalAlignment" Text="{Binding Type.Text}"/>
</Grid>
文本框模板
<ControlTemplate x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
<ControlTemplate.Resources>
<Storyboard x:Key="OnLoaded1">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="PART_ContentHost">
<EasingColorKeyFrame KeyTime="0" Value="#FF6A7FCE"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnGotFocus1"/>
</ControlTemplate.Resources>
<Border BorderBrush="#FF494949" BorderThickness="1" CornerRadius="2">
<ScrollViewer x:Name="PART_ContentHost" Background="#FFC1C5D4" FontFamily="Arial" BorderThickness="0"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="FocusManager.GotFocus">
<RemoveStoryboard BeginStoryboardName="OnLoaded1_BeginStoryboard"/>
<BeginStoryboard x:Name="OnLoaded1_BeginStoryboard" Storyboard="{StaticResource OnLoaded1}"/>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>