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

发布于 2024-10-09 14:59:11 字数 3468 浏览 6 评论 0原文

为了显示 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>

正如您可能猜到的,值转换器负责将某些值转换为画笔。它适用于此 但不适用于列表框项目。

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

你有主意吗?

提前致谢。

To display a WPF list box, I use a user control (MessageRowTemplate) as list box item. The user contrul uses a value converter (MessageTypeToBrushConverter). For some reason, the value converter is only trigered if the user control isn't used as list box item but seperate.

xaml-file with user control used seperately and inside a list box as list box item:

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

And here's the 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>

As you might guess, the value converter is responsible for converting some value to a brush. It works fine for this <local:MessageRowTemplate x:Name="questionControl" /> but not for the list box item.

Google didn't give me any hints for keyword combinations of 'list box', 'list box item', 'user control', 'value converter'.

Do you have an idea?

Thanks in advace.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-10-16 14:59:11

该列表框

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

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

The list box

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

uses the local resource ResponsesListTemplat which again uses the MessageRowTemplate.
The converter is used within this template:

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

and applied to the first border-tag within the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文