0

UI のリソース セクションでいくつかの画像を定義しています。

<Window.Resources>
    <!-- Converters -->
    <loc:UserStatusToIconConverter x:Key="UserStatusToIconConverter" />

    <!-- Images -->
    <BitmapImage x:Key="ConnectIcon" UriSource="/WPFClient;component/Images/connect.png" />
    <BitmapImage x:Key="ActiveIcon" UriSource="/WPFClient;component/Images/active.png" />
    <BitmapImage x:Key="IdleIcon" UriSource="/WPFClient;component/Images/idle.png" />
    <BitmapImage x:Key="AwayIcon" UriSource="/WPFClient;component/Images/away.png" />
    <BitmapImage x:Key="UnknownIcon" UriSource="/WPFClient;component/Images/unknown.png" />
...

これらのいずれかをコンバーターのバインディングに選択したいと思います。これは、コンバーターから毎回 (500 回) 新しいイメージを作成するよりも効率的であると思います。

public class UserStatusToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string userStatus = value.ToString();
        string iconName = ...;

        switch (userStatus)
        {
            case "Active":
                // select ActiveIcon;
                break;
            case "Idle":
                // select IdleIcon;
                break;
            case "Away":
                ...
                break;
        }

        return iconName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

ここで私はそれを使用します:

            <ListBox ItemsSource="{Binding Users}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <DockPanel>
                            <Image Source="{Binding Status, Converter={StaticResource UserStatusToIconConverter}}" Height="16" Width="16" />
                            <TextBlock Text="{Binding Nick}" />
                        </DockPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
4

2 に答える 2

3

DataTemplate.Triggersこの場合、コンバーターではなく使用する方がよいと思います。

                 <DataTemplate>
                    <DockPanel>
                        <Image x:Name="Img" Height="16" Width="16" />
                        <TextBlock Text="{Binding Nick}" />
                    </DockPanel>

                    <DataTemplate.Triggers>
                       <DataTrigger Binding="{Binding Status}" Value="Active">
                          <Setter TargetName="Img" Property="Source" Value="{StaticResource ActiveIcon}"/>
                       </DataTrigger>

                       <DataTrigger Binding="{Binding Status}" Value="Idle">
                          <Setter TargetName="Img" Property="Source" Value="{StaticResource IdleIcon}"/>
                       </DataTrigger>

                       <!-- And So on... -->

                    </DataTemplate.Triggers>
                </DataTemplate>
于 2013-08-20T17:14:59.260 に答える
0

Convert メソッドで次の操作を行うだけです。

return Application.Current.MainWindow.FindResource(iconName);
于 2013-08-20T17:14:24.210 に答える