벽에 머리를 때려서 질문을 올릴 시간이라고 생각했습니다. 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에 대한 바인딩이 작동하지만 내 컨트롤에 대한 바인딩이 작동하지 않는 이유는 무엇입니까? 내가 도대체 뭘 잘못하고있는 겁니까?
시도의 DataContext를 추가 =이; 귀하의 TimeControl 생성자에서. – eandersson