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();
}
在我使用该控件的窗口中,它在网格中使用,其数据上下文设置为 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>
现在,绑定到 Path ShiftName 的 TextBox 工作正常并显示文本。当我使用我的控件时,它不会显示预期的文本。但是,在第二个示例中,我只是将文本依赖属性设置为“THIS WORKS OK”,文本按预期显示。为什么绑定对 TextBox 有效,但对我的控件无效。我做错了什么?
Been beating my head against a wall and figured it was time to post a question. I have the following user control
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();
}
In the Window where i use the control it is used in a grid with its datacontext set to a 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>
Now the TextBox that is Bound to Path ShiftName works fine and displays the text. When i use my control instead it doesn't show the expected text. However in the second example where i just set the Text dependency property to "THIS WORKS OK" the text shows up as expected. Why does the binding work for the TextBox but not my control. What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已将
UserControl
上的DataContext
设置为自身。因此,TimeControl
的Text
属性的绑定尝试在TimeControl
上查找名为ShiftName
的属性,不在您的静态资源上。如果您查看 Visual Studio 的输出窗口,您应该会看到一条错误消息。无论如何,在
UserControl
上设置DataContext
从来都不是一个好主意,因为任何人都可以覆盖它。你可以这样做:You've set the
DataContext
on theUserControl
to itself. Thus, the binding of yourTimeControl
'sText
property is attempting to find a property calledShiftName
on yourTimeControl
, not on your static resource. If you look in the output window of visual studio, you should see an error message to that effect.It is never a good idea to set the
DataContext
on theUserControl
anyway, because anyone could override it. You could just do this instead:另一种方法可能是使用相对绑定:
An other way might be to use a Relative binding: