2011-07-17 145 views
0

我在WPF中有以下示例代碼。當我將xaml和相應的C#代碼放在窗口中時,它運行良好,但是當我使用用戶控件時,數據綁定不起作用。爲什麼?用戶控件中的數據綁定不起作用(WPF)

下面是代碼:

用戶控制XAML:

<UserControl x:Class="TestWpf.UserControl1" 
     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:ec="clr-namespace:TestWpf" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid x:Name="MainGrid" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="20"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <WrapPanel HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Row="0" > 
     <Button Content="Edit" HorizontalAlignment="Right" Name="editButton1" VerticalAlignment="Stretch" Click="button1_Click" /> 
    </WrapPanel> 
    <Grid x:Name="patientDataGrid" Margin="10,10,10,10" Grid.Row="1"> 
     <Grid.Resources> 
      <Style TargetType="TextBox" > 
       <Setter Property="IsEnabled" Value="{Binding Path=IsEditing, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ec:UserControl1}}}" /> 
      </Style> 
     </Grid.Resources> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 

     </Grid.ColumnDefinitions> 
     <TextBox x:Name="textBox1" Grid.Column="0" Text="!234" /> 
     <TextBox x:Name="textBox2" Grid.Column="1" /> 
    </Grid> 
</Grid> 
</UserControl> 

用戶控件C#代碼:

public partial class UserControl1 : UserControl 
{ 
    public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
      "IsEditing", typeof(Boolean), typeof(MainWindow), new PropertyMetadata(false)); 

    public Boolean IsEditing 
    { 
     get { return (Boolean)GetValue(IsEditingProperty); } 
     set { SetValue(IsEditingProperty, value); } 
    } 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     IsEditing = !IsEditing; 
    } 
} 

MainWindows XAML:

<Window x:Class="TestWpf.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ec="clr-namespace:TestWpf" 
    Title="MainWindow" Height="200" Width="525"> 

    <ec:UserControl1 x:Name="c" /> 

</Window> 

我注意到如果我是將所有C#和xaml代碼從用戶控制轉換到MainWindows,綁定工作沒有任何問題。

代碼有什麼問題?

回答

2

您註冊的依賴項屬性由MainWindow擁有,而不是您的UserControl。

public static readonly DependencyProperty IsEditingProperty = 
    DependencyProperty.Register 
    (
     "IsEditing", 
     typeof(Boolean), 
     typeof(MainWindow), // <-- Here 
     new PropertyMetadata(false) 
    ); 
+0

謝謝。我正在寫一個測試項目來查找一個綁定問題,我在這個問題中詢問:http://stackoverflow.com/questions/6647460/problem-in-wpf-binding – mans

0

這是因爲您錯誤地註冊了IsEditingProperty。您必須註冊UserControl1,而不是MainWindow

public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
     "IsEditing", typeof(Boolean), typeof(UserControl1), new PropertyMetadata(false));