可以用UserControl进行“简单”绑定吗?

时间:2013-04-09 17:23:00

标签: wpf binding user-controls

我有一个用户控件,其中有一些文本块。我想将此usercontrol包含到listBox(或listview中,以防它出现任何问题)。

当我检查输出窗口时,我看到没有绑定异常,但我也没有在文本块中看到任何内容。

有没有让这项工作?

谢谢:

这是我现在使用的listBox:

<ListBox AllowDrop="True"  Grid.Row="1" 
         Style="{StaticResource BaseListBox}" x:Name="LstEquipeDefaut">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <my:ucEquipe x:Name="ucEquipe" Grid.Row="1" Margin="5,0,5,2"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是Usercontrol:

<UserControl x:Class="ucEquipe"
         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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
         mc:Ignorable="d"       
         d:DesignHeight="350" d:DesignWidth="180" MinWidth="180" >
<Border Style="{StaticResource UControlBorder}">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="32" />
            <RowDefinition Height="25" />
            <RowDefinition Height="35" />
            <RowDefinition Height="100" />
            <RowDefinition Height="35" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <TextBox AllowDrop="True" x:Name="TxtChiefEquipe" 
                 Style="{StaticResource BaseTextBox}" 
                 Text="{Binding Mode=OneWay,Path=chefEquipe.NomComplet}" 
                 Grid.Row="1"  />           
    </Grid>
</Border>

这是我使用的对象:

Public Class Equipe

Public Property ID As Long = 0
Public Property Couleur As String = ""
Public Property Semaine As New Date(1900, 1, 1)
Public Property chefEquipe As Employe = Nothing

Public Property ListEquipeEmploye As New List(Of EquipeEmploye)
Public Property ListEquipeEquipement As New List(Of EquipeEquipement)

End Class

objet Employe有一个名为NomComplet的财产。现在我手动在列表框中添加了新对象进行测试。

1 个答案:

答案 0 :(得分:1)

您的Equipe课程需要实施INotifyPropertyChanged

private Employe _chefEquipe;
public Employe ChefEquipe
{
    get { retun _chefEquipe; }
    set 
    {
        _chefEquipe = value;
        NotifyPropertyChanged("ChefEquipe");
    }
}

抱歉C#,我不记得VB语法了=)