2011-03-31 5 views
2

벽에 머리를 때려서 질문을 올릴 시간이라고 생각했습니다. I는 다음과 같은 사용자 제어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(); 
    } 

정적 리소스 소스

<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에 바인딩 된 TextBox가 올바르게 작동하고 텍스트가 표시됩니다. 대신 내 컨트롤을 사용할 때 예상 텍스트가 표시되지 않습니다. 그러나 두 번째 예제에서는 "이 작동 확인"텍스트 종속성 속성을 설정하면 텍스트를 예상대로 나타납니다. TextBox에 대한 바인딩이 작동하지만 내 컨트롤에 대한 바인딩이 작동하지 않는 이유는 무엇입니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+1

시도의 DataContext를 추가 =이; 귀하의 TimeControl 생성자에서. – eandersson

답변

6

UserControlDataContext을 설정했습니다. 따라서 TimeControlText 속성을 바인딩하면 TimeControl에서 ShiftName이라는 속성을 찾고 정적 리소스를 찾지 못했습니다. Visual Studio 출력 창을 보면 오류 메시지가 나타납니다.

어쨌든 UserControlDataContext을 설정하는 것은 좋지 않습니다. 누구든지이를 무시할 수 있기 때문입니다. 당신은 단지 대신이 작업을 수행 할 수 있습니다 :

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

그게 해결되었습니다 ... 많은 감사합니다. – MikeC

0

타 방법은 상대 바인딩을 사용할 수 있습니다 :

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