ref*_*l3x 2 c# wpf xaml user-controls mvvm
我的项目是用MVVM实现的。我有一个主窗口,它由状态栏和选项卡视图组成。在选项卡视图内,有一个名为“AnnotationView”的用户控件。Annotationview 是两个较小的用户控件(称为 TimePicker)的父控件。TimePicker 由两个文本框组成,一个用于小时,一个用于分钟。我想使用此 UserControl 两次(这也是我将其设为自己的 Control 的原因,以便稍后重用它)。
TimePicker 的 XAML:
<UserControl x:Class="archidb.Views.TimePicker"
x:Name="TimePickerControl"
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" Height="Auto" Width="Auto"
KeyboardNavigation.TabNavigation="Local">
<Grid DataContext="{Binding ElementName=TimePickerControl}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="5"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Style="{StaticResource TextBoxStyle}"
Text="{Binding Path=HourValue}"
x:Name="tbHours"
KeyboardNavigation.TabIndex="0"/>
<TextBlock Style="{StaticResource TextBlockStyle}"
Margin="0 -3 0 5"
Text=":"
Grid.Column="1"/>
<TextBox Grid.Column="2" Style="{StaticResource TextBoxStyle}"
Text="{Binding Path=MinuteValue}"
x:Name="tbMinutes"
KeyboardNavigation.TabIndex="1"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
TimePicker 代码隐藏:
public partial class TimePicker : UserControl
{
public static readonly DependencyProperty HourValueProperty = DependencyProperty.Register("HourValue", typeof(string), typeof(TimePicker), new PropertyMetadata("00"));
public string HourValue
{
get { return (string)GetValue(HourValueProperty); }
set { SetValue(HourValueProperty, value); }
}
public static readonly DependencyProperty MinuteValueProperty = DependencyProperty.Register("MinuteValue", typeof(string), typeof(TimePicker), new PropertyMetadata("00"));
public string MinuteValue
{
get { return (string)GetValue(MinuteValueProperty); }
set { SetValue(MinuteValueProperty, value); }
}
public TimePicker()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)
在 AnnotationControl 中,我像这样插入 UserControls:
<v:TimePicker x:Name="tpStart"
HourValue="{Binding Path=StartHours}"
MinuteValue="{Binding Path=StartMinutes}"
KeyboardNavigation.TabIndex="2"/>
<v:TimePicker x:Name="tpEnde"
HourValue="{Binding Path=EndHours}"
MinuteValue="{Binding Path=EndMinutes}"
KeyboardNavigation.TabIndex="3"
Grid.Row="2"/>
Run Code Online (Sandbox Code Playgroud)
AnnotationControl 的 DataContext 设置为其视图模型,我在其中声明了属性。
问题是,绑定不起作用。我在依赖属性(“00”)中设置的默认值未显示在任何文本框中。此外,如果我在文本框中写入内容,AnnotationControl 的视图模型中的属性不会更改其值。这个问题已经困扰我好几天了,我在这里做错了什么?
RelativeSource Binding解决方案是简单地在您的中使用 aUserControl并且不将其设置DataContext为自身:
在你的掌控之中:
<TextBox Style="{StaticResource TextBoxStyle}" Text="{Binding HourValue, RelativeSource=
{RelativeSource AncestorType={x:Type YourPrefixToBeAdded:TimePickerControl}}}"
x:Name="tbHours" KeyboardNavigation.TabIndex="0" />
<TextBlock Style="{StaticResource TextBlockStyle}" Margin="0 -3 0 5" Text=":"
Grid.Column="1" />
<TextBox Grid.Column="2" Style="{StaticResource TextBoxStyle}" Text="{Binding
MinuteValue, RelativeSource={RelativeSource AncestorType={x:Type
YourPrefixToBeAdded:TimePickerControl}}}" x:Name="tbMinutes"
KeyboardNavigation.TabIndex="1" />
Run Code Online (Sandbox Code Playgroud)
然后您将能够从控件外部对属性进行数据绑定。
| 归档时间: |
|
| 查看次数: |
4471 次 |
| 最近记录: |