2011-03-31 67 views
2

一直在我的头靠在墙上,并认为是时候发布一个问题。我有以下用户控制问题WPF的数据绑定

XAML

<UserControl x:Class="WorkDayManager3.WPF.UserControls.TimeControl" 
     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" 
     d:DesignHeight="46" d:DesignWidth="133" Background="Transparent" 
     DataContext="{Binding RelativeSource={RelativeSource self}}">  
<Grid Height="44" Width="132" Background="Transparent"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="29" />    
    </Grid.ColumnDefinitions> 

    <TextBox Name="label" Text="{Binding Text}" Grid.ColumnSpan="5"></TextBox> 

</Grid> 

C#

public partial class TimeControl : UserControl 
{ 
    public string Text 
    { 

     get { return (string)GetValue(TextProperty); } 

     set { SetValue(TextProperty, value); } 

    } 

    // Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... 

    public static readonly DependencyProperty TextProperty = 

    DependencyProperty.Register("Text", typeof(string), typeof(TimeControl), new UIPropertyMetadata("N/A")); 

    public TimeControl() 
    { 
     InitializeComponent(); 
    } 

在我使用它与其的datacontext网格设定为所使用的控制窗口StaticResource

<Grid Grid.Row="1" HorizontalAlignment="Center" DataContext="{StaticResource shiftViewSource}" Name="grid1" Margin="48,10,38,0"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <TextBox Grid.Column="0" Grid.Row="1" Height="23" TabIndex="2" HorizontalAlignment="Left" Margin="10,5,0,5" Name="shiftNameTextBox" Text="{Binding Path=ShiftName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /> 
     <my2:TimeControl Grid.Column="0" Grid.Row="2" Height="23" TabIndex="2" HorizontalAlignment="Left" Margin="10,5,0,5" Name="shiftNameTextBox" Text="{Binding Path=ShiftName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="120" /> 
     <my2:TimeControl Grid.Column="0" Grid.Row="3" Text="THIS WORKS OK"/>    
    </Grid> 

现在,绑定到路径ShiftName的文本框可正常工作并显示文本。当我使用我的控件时,它不显示预期的文本。然而,在第二个例子中,我只是将文本依赖项属性设置为“这工作正常”文本显示如预期。为什么绑定工作的文本框,但不是我的控制。我究竟做错了什么?

+1

尝试增加的DataContext =这一点;在你的TimeControl构造函数中。 – eandersson 2011-03-31 17:27:10

回答

6

您已经在UserControl上设置了DataContext自己。因此,您的TimeControlText属性的绑定尝试在您的TimeControl上找到名为ShiftName的属性,而不是在您的静态资源上。如果您查看visual studio的输出窗口,您应该看到一个错误消息。

无论如何,在UserControl上设置DataContext绝不是一个好主意,因为任何人都可以覆盖它。你可能只是这样做,而不是:

<UserControl x:Name="root" ...> 
    <Grid DataContext="{Binding ElementName=root}" 
     ... 
+0

解决了它......非常感谢 – MikeC 2011-03-31 19:05:38

0

一种其他的方式可能是使用的相对结合:

<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" > 
    ...