如果用作列表框项,则不会在用户控件内触发值转换器

时间:2010-12-28 17:05:23

标签: c# wpf user-controls datatemplate valueconverter

要显示WPF列表框,我使用用户控件(MessageRowTemplate)作为列表框项。用户控件使用值转换器(MessageTypeToBrushConverter)。出于某种原因,如果用户控件不用作列表框项但是单独使用,则值转换器仅会被触发。

带有用户控件的xaml文件单独使用,并在列表框内作为列表框项:

<local:MuuriWindow x:Class="Muuri_UI_WPFv3.Window_Conversation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Muuri_UI_WPFv3" 
Loaded="EventHandler_WindowLoaded">
<Window.Resources>
    <DataTemplate x:Key="ResponsesListTemplate">
        <local:MessageRowTemplate Margin="3" />
    </DataTemplate>
    <local:MessageTypeToBrushConverter x:Key="converter" />
</Window.Resources>
<StackPanel Orientation="Vertical">
    <local:MessageRowTemplate x:Name="questionControl" />
    <ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource ResponsesListTemplate}" ItemsSource="{Binding}"  Name="listBoxResponses" Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource MuuriListboxStyle}" />
    <StackPanel Orientation="Horizontal">
        <Button Name="buttonReply" Click="EventHandler_ButtonReplyClicked">Reply</Button>
        <Button Name="buttonCancel" Click="EventHandler_ButtonCancelClicked">Cancel</Button>
    </StackPanel>
</StackPanel>
</local:MuuriWindow>

这是MessageRowTemplate.xaml:

<UserControl x:Class="Muuri_UI_WPFv3.MessageRowTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Muuri_UI_WPFv3" 
Width="300"
Loaded="UserControl_Loaded">
<UserControl.Resources>
    <local:MessageTypeToBrushConverter x:Key="converter" />
</UserControl.Resources>
<Grid Margin="2,1,2,1" VerticalAlignment="Top" Height="auto">
    <Grid.RowDefinitions>
        <RowDefinition Height="2" />
        <RowDefinition Height="0.5*" />
        <RowDefinition Height="0.5*" />
        <RowDefinition Height="2" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="15"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Border Name="leftBorder" Margin="2,2,0,2" Grid.RowSpan="4" CornerRadius="3,0,0,3" Panel.ZIndex="1" Background="{Binding Path=Type, Converter={StaticResource converter}}" />
    <Border Margin="0,0,0,0" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="4" Background="#FF666666" CornerRadius="0,5,5,0" />
    <Border Margin="0,0,0,0" Grid.ColumnSpan="2" Grid.RowSpan="4" Background="{x:Null}" BorderThickness="2,2,2,2" BorderBrush="#FF202020" CornerRadius="5,5,5,5" Panel.ZIndex="0" />
    <TextBlock Padding="5,2,5,2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Text="{Binding Path=Text}" Grid.Row="1" Grid.Column="1" Foreground="White" />
    <TextBlock Padding="5,2,5,2" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Path=Author}" Grid.Row="2" Grid.Column="1" Foreground="White" />
</Grid>
</UserControl>

正如您可能猜到的,值转换器负责将某些值转换为画笔。它适用于此<local:MessageRowTemplate x:Name="questionControl" />但不适用于列表框项。

谷歌没有给我任何关于'列表框','列表框项','用户控制','价值转换器'的关键字组合的提示。

你有什么想法吗?

先谢谢。

1 个答案:

答案 0 :(得分:0)

列表框

<ListBox HorizontalAlignment="Stretch" ItemTemplate="{DynamicResource ResponsesListTemplate}" ItemsSource="{Binding}"  Name="listBoxResponses" Height="200" ScrollViewer.VerticalScrollBarVisibility="Visible" Style="{StaticResource MuuriListboxStyle}" />

使用本地资源ResponsesListTemplat,它再次使用MessageRowTemplate。 转换器在此模板中使用:

<UserControl.Resources>
    <local:MessageTypeToBrushConverter x:Key="converter" />
</UserControl.Resources>

并应用于模板中的第一个border-tag。