wpf - Listview DataTemplate inter UserControl 无法绑定
问题描述
我有这个问题:
我ListView
在表单中有一个控件。这里面有一个CustomControl
。我想让这个自定义控件绑定到当前窗体的ViewModel中的Time属性,但是绑定不了,但是中间的Other控件可以绑定,这是怎么回事?我将不胜感激DataTemplate
ListView
DataTemplate
这是我的窗口:
<ListView Grid.Row="2"
ItemsSource="{Binding PlanRotation}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20 0 0 0" Orientation="Horizontal">
<TextBlock HorizontalAlignment="Center" Text="时间:" VerticalAlignment="Center" FontWeight="Bold" />
<TimeControl:DateTimePicker Tag="{Binding ElementName=myWin,Path=DataContext.Time,Mode=OneWayToSource}"
HorizontalAlignment="Center"
Height="28"
VerticalAlignment="Center"
Width="152"
Foreground="White"
BorderThickness="1"
BorderBrush="#FFABADB3"
Background="Black"
Grid.Column="1"/>
<ComboBox Text="{Binding ElementName=myWin,Path=DataContext.Scenes,Mode=OneWayToSource}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="80"
Height="21">
</ComboBox>
这是我的自定义控件TimeControl:DateTimePicker
代码:
<UserControl x:Class="ManagementProject.UserControls.TimeControl.DateTimePicker"
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"
xmlns:local="clr-namespace:ManagementProject.UserControls.TimeControl"
xmlns:myTime="clr-namespace:ManagementProject.UserControls.TimeControl"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="25"
d:DesignWidth="150"
Width="150"
MaxHeight="25"
x:Name="dtpName"
Loaded="UserControl_Loaded" Tag="{Binding ElementName=textBlock1, Path=Text}">
<TextBox
Height="23"
HorizontalAlignment="Left"
Text="{Binding ElementName=dtpName,Path=Tag,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}"
Margin="4,3,0,0"
Name="textBlock1"
VerticalAlignment="Top"
Width="123" Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" SelectionBrush="{x:Null}" Style="{DynamicResource TextBoxStyle1}" />
我的自定义控件TextBox
的文本是在后台分配的。
解决方案
我只能在初始化时直接绑定以使用用户控件。我最终创建了一个依赖属性,以便能够从外部更新用户控件。
internal List<User> ContactlistContainer
{
get => (List<User>)GetValue(ContactlistContainerProperty);
set => SetValue(ContactlistContainerProperty, value);
}
internal static readonly DependencyProperty ContactlistContainerProperty =
DependencyProperty.Register("ContactlistContainerProperty", typeof(List<User>), typeof(User), new PropertyMetadata(new List<User>()));
推荐阅读
- python - 使用python中的txt文件中的数据进行计算
- python - 在 VS Code 中以 # 或 ## 开头的注释是否可以有不同的颜色和字体设置?
- java - 使用按钮添加开关 - Android Studio
- javascript - react-location-picker中未定义react下一个应用程序获取错误窗口
- clang - getelementptr llvm ir 指令参数
- azure - 写操作(每 4MB,每 10000 次)是什么意思?
- python - 将前导零添加到日期和时间字符串中的小时数
- spring-boot - Jersey 如何从请求中屏蔽密码
- python - 获取在函数内部创建的变量值
- sql - SQLite Sum 和 CASE 语句