首页 > 解决方案 > Listview DataTemplate inter UserControl 无法绑定

问题描述

我有这个问题:

ListView在表单中有一个控件。这里面有一个CustomControl。我想让这个自定义控件绑定到当前窗体的ViewModel中的Time属性,但是绑定不了,但是中间的Other控件可以绑定,这是怎么回事?我将不胜感激DataTemplateListViewDataTemplate

这是我的窗口:

    <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的文本是在后台分配的。

标签: wpflistviewdata-bindinguser-controlsdatatemplate

解决方案


我只能在初始化时直接绑定以使用用户控件。我最终创建了一个依赖属性,以便能够从外部更新用户控件。

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>()));

推荐阅读