WPF UserControl - 无法选择TextBox

时间:2008-11-06 18:32:35

标签: wpf xaml user-controls

我有一个非常简单的WPF UserControl:

<UserControl x:Class="dr.SitecoreCompare.WPF.ConnectionEntry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Name="connEntry" 
BorderBrush="Navy" BorderThickness="1" Margin="5,0,0,5" >
<StackPanel Margin="0,10,0,0" >
    <Label FontWeight="ExtraBold" Content="{Binding ElementName=connEntry, Path=Title}"></Label>
    <Label Margin="0,5,0,0">Server:</Label>
    <TextBox x:Name="txtServer" TabIndex="1" Text="{Binding Path=ServerName}" ></TextBox>
    <Label>Database:</Label>
    <TextBox x:Name="txtDatabase" TabIndex="2" Text="{Binding Path=DatabaseName}"></TextBox>
</StackPanel>

在同一窗口中使用两次。现在,我可以在UserControl的两个实例上选择第一个TextBox,但是不能通过Tab键或单击选择第二个(“txtDatabase”)文本框。为什么是这样 ?我是否遗漏了有关创建WPF用户控件的内容?

编辑: DatabaseName不是readonly,它是一个简单的属性。放置usercontrol的窗口的XAML如下所示:

<Window x:Class="dr.SitecoreCompare.WPF.ProjectDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:dr.SitecoreCompare.WPF"       
    Title="Choose project" Height="280" Width="500" 
    WindowStartupLocation="CenterOwner" WindowStyle="SingleBorderWindow" HorizontalAlignment="Center" ShowInTaskbar="False" ShowActivated="True" ResizeMode="NoResize" VerticalContentAlignment="Top" VerticalAlignment="Center">
    <StackPanel>
        <Label>Choose databases</Label>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <c:ConnectionEntry Grid.Column="0" x:Name="connMaster" Title="Master:" Padding="5" />
            <c:ConnectionEntry Grid.Column="1" x:Name="connSlave"  Title="Slave:"  Padding="5" />
        </Grid>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0" >
            <Button x:Name="btnCancel" Click="btnCancel_Click">Cancel</Button>
            <Button x:Name="btnOK" Click="btnOK_Click">OK</Button>
        </StackPanel>
    </StackPanel>

</Window>

2 个答案:

答案 0 :(得分:2)

在绑定中尝试Mode = TwoWay。我已经看到了初始化设置值的地方,控件无法设置值。

<TextBox x:Name="txtDatabase" TabIndex="2" Text="{Binding Path=DatabaseName, Mode=TwoWay}"></TextBox>

答案 1 :(得分:0)

这适用于XamlPad,所以我认为你发布的代码之外还有一些导致问题的东西。 DatabaseName是readonly吗?