簡體   English   中英

如果用作列表框項目,則不會在用戶控件內觸發值轉換器

[英]Value converter is not triggered within user control if used as list box item

為了顯示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" />它工作正常,但不適用於列表框項目。

Google沒有給我任何有關“列表框”,“列表框項”,“用戶控件”,“值轉換器”的關鍵字組合的提示。

你有想法嗎?

非常感謝。

列表框

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM