1

为了显示 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" />,但不适用于列表框项目。

谷歌没有给我任何关于“列表框”、“列表框项”、“用户控件”、“值转换器”的关键字组合的提示。

你有想法吗?

预先感谢。

4

1 回答 1

0

列表框

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

使用再次使用 MessageRowTemplate 的本地资源 ResponsesListTemplat。该模板中使用了转换器:

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

并应用于模板中的第一个边框标签。

于 2010-12-28T17:28:31.073 回答