2008-11-06 38 views
1

我有一個非常簡單的WPF用戶控件:WPF用戶控件 - 不能選擇文本框

<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,但不能通過選項卡或單擊選擇第二個(「txtDatabase」)文本框。爲什麼是這樣 ?我錯過了一些關於創建WPF usercontrols的事情嗎?

編輯: DatabaseName不是隻讀的,它是一個簡單的屬性。該窗口的用戶控件放置在看起來像這樣的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

Try Mode = TwoWay in your binding。我已經看到了初始化設置值的地方,控件不能設置值。

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

這工作在XamlPad,所以我覺得這是你發佈,是造成該問題的代碼之外的東西。 DatabaseName是否只讀?