【问题标题】:It is possible to do 'simple' binding with a UserControl?可以使用 UserControl 进行“简单”绑定吗?
【发布时间】:2013-04-09 17:22:59
【问题描述】:

我有一个用户控件,其中有一些文本块。我想将此用户控件包含到一个列表框(或列表视图中,以防它引起任何问题)。

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

有没有办法让这个工作?

谢谢:

这是我现在使用的列表框:

<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 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

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

【问题讨论】:

  • 此属性chefEquipe.NomComplet 位于何处?
  • 对...忘记放我用的对象

标签: wpf binding user-controls


【解决方案1】:

您的Equipe 类需要实现INotifyPropertyChanged

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

对不起 C#,我不记得 VB 语法了 =)

【讨论】:

  • 其实我发现了问题。请求帮助的人忘记提及文本块在资源文件中是不可见的......但你是对的,最好实现这一点。
猜你喜欢
  • 2022-01-20
  • 2011-03-29
  • 2017-05-23
  • 2011-05-13
  • 1970-01-01
  • 2013-03-06
  • 2012-02-06
  • 1970-01-01
  • 2011-06-12
相关资源
最近更新 更多