设置依赖项属性值时出错

时间:2013-01-30 16:12:59

标签: c# .net wpf xaml

我有一个" RoomType" class,我创建了一个用户控件,可以将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 Exception",我已经逐步调试,并且不存在无限循环。

这是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>

TextBox模板

<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>

0 个答案:

没有答案