我在App.xaml中有一个DataTemplate:
<DataTemplate DataType="{x:Type local2:DirectoryFilesViewModel}">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Height="Auto">
<TextBlock DockPanel.Dock="Top"><Hyperlink Style="{StaticResource ButtonHype}"
Command="{Binding ClickCommand}"><InlineUIContainer>
<Image Source="{Binding FolderImage}" VerticalAlignment="Center" Stretch="Uniform"
RenderOptions.BitmapScalingMode="HighQuality" RenderTransformOrigin=".5,.5"
ToolTip="Disc" Margin="7,9,0,0" Height="27" />
</InlineUIContainer></Hyperlink></TextBlock>
<TextBlock Text="{Binding DiscName}" FontSize="2" Margin="8,10,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.7" Storyboard.TargetProperty="FontSize" To="20" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
<ListBox ItemsSource="{Binding DiscFiles}" >
<ListBox.ItemTemplate>
<DataTemplate>
<controls:MemberListItem /> // Need to bind to a TextBlock in this Control
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
/DataTemplate>
一切都很好,除了ListBox UserControl ...似乎无法绑定到其中的TextBlock。有人能指出我正确的方向吗?
用户控件:
<UserControl x:Class="Music.Controls.MemberListItem"
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:local2="clr-namespace:MusicMurfer.ViewModels"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="300" Height="32" Width="Auto">
<!--<UserControl.DataContext>
<local2:MemberListViewModel />
</UserControl.DataContext>-->
<Grid VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="0" Grid.Column="0"
Source="{Binding DiscFileImage}" RenderOptions.BitmapScalingMode="HighQuality" Height="15"
Margin="-2,5,5,5" Stretch="Uniform" RenderTransformOrigin=".5,.5" />
<TextBlock Grid.Column="1" Width="Auto" Foreground="White" TextAlignment="Left"
VerticalAlignment="Center" FontSize="6" x:Name="FileText" Margin="5,5,5,5"
Height="Auto" ToolTip="Click to Play" Text="{Binding ThisDoesntWorkYet}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<EventTrigger RoutedEvent="Loaded">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.4" Storyboard.TargetProperty="FontSize" To="16" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
SO主要抱怨代码 - 需要详细信息
答案 0 :(得分:0)
在实际的&#34; MemberListItem&#34;在控件中,您需要将TextBox的text属性绑定到将出现在用户控件的datacontext上的数据成员(这是DiscFiles集合中的特定成员)。
因此,如果您的用户控件类似于:
<UserControl>
<TextBlock Text="{Binding DiscFileName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</UserControl>
替换&#34; DiscFileName&#34;使用&#34;名称&#34; DiscFile对象上的属性。您不会将文本绑定放在DataTemplate中的用户控件的实际实例上。