WPF ListBoxItems问题

时间:2010-11-03 19:31:18

标签: wpf styles listboxitems

在wpf应用程序中,我将词典绑定到列表框。我在列表框上有自己的风格。 列表框项目consit图像和3个texblocks。

朋友班在这里:

public class FriendData
{
    public string idUser { get; set; }
    public string nick { get; set; }
    public string sefNick { get; set; }
    public string status { get; set; }
    public string photo { get; set; }
    public string sex { get; set; }
    public string isFriend { get; set; }

    public string blockQuote { get; set; }

    public FriendData(string idUser, string nick, string sefNick, string status, string photo, string sex, string isFriend)
    {
        this.idUser = idUser;
        this.nick = nick;
        this.sefNick = sefNick;
        this.status = status;
        this.photo = photo;
        this.sex = sex;
        this.isFriend = isFriend;
    }
}

ListBox样式在这里:

    <Style x:Key="friendsListStyle" TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid Name="MainGrid">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="0.3*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="60"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Image Source="{Binding Value.photo}" Margin="4,4,4,2"/>
                        <Grid Name="SlaveGrid" Grid.Column="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"></RowDefinition>
                                <RowDefinition Height="*"></RowDefinition>
                                <RowDefinition Height="*"></RowDefinition>
                            </Grid.RowDefinitions>
                            <TextBlock Name="tbName" Text="{Binding Value.nick}" Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" FontSize="13" FontWeight="Medium"></TextBlock>
                            <TextBlock Name="tbBlockQuote" Text="{Binding Value.blockQuote}" Grid.Column="0" Grid.Row="1" Margin="2,2,2,2" FontSize="11" FontWeight="Normal" Foreground="DarkGray"></TextBlock>
                            <TextBlock Name="tbStatus" Text="{Binding Value.status}" Grid.Column="0" Grid.Row="2" Margin="2,2,2,2" FontSize="11" FontWeight="Normal"></TextBlock>
                        </Grid>
                    </Grid>
                    <DataTemplate.Triggers>
                        <!--<Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="LayoutTransform" TargetName="MainGrid">
                                <Setter.Value>
                                    <ScaleTransform ScaleX="1.35" ScaleY="1.35" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>-->
                    </DataTemplate.Triggers>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的第一个问题是,tbStatus绑定到类Friend的属性状态。状态只能有3个值:0,1,2。如果status的值为0,我需要设置tbStatus.Text =“Offline”,如果status的值为1,我需要设置tbStatus.Text =“Online”,如果status为2则必须是tbStatus.Text =“Just Log”。可能这个条件是从listBox样式的XAML设置的吗?

<TextBlock Name="tbStatus" Text="{Binding Value.status}" Grid.Column="0" Grid.Row="2" Margin="2,2,2,2" FontSize="11" FontWeight="Normal"></TextBlock>

我的第二个问题是如果状态为0,我需要将Image转换为灰度。我有功能转换为灰度。我想在listBox样式中设置这些条件。任何进步?

这是:

private void ConvertImageToGrayScaleImage(string uri)

{

    Image grayImage = new Image();

    BitmapImage bmpImage = new BitmapImage();

    bmpImage.BeginInit();

    bmpImage.UriSource = new Uri(uri);

    bmpImage.EndInit();

    FormatConvertedBitmap grayBitmap = new FormatConvertedBitmap();

    grayBitmap.BeginInit();

    grayBitmap.Source = bmpImage;

    grayBitmap.DestinationFormat = PixelFormats.Gray8;

    grayBitmap.EndInit();

    grayImage.Source = grayBitmap;

    LayoutRoot.Children.Add(grayImage);

}

1 个答案:

答案 0 :(得分:1)

在这两种情况下,最好的办法是写一个IValueConverter

例如,这里有一个用于转换文字的内容:

public class StatusToTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        int status = Int32.Parse(value.ToString());
        switch (status)
        {
            case 0:
                return "Offline";

            case 1:
                return "Online";

            case 2:
                return "Just Log"; 
        }

        return Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string status = value.ToString();
        switch (status)
        {
            case "Offline":
                return 0;

            case "Online":
                return 1;

            case "Just Log":
                return 2; 
        }

        return Binding.DoNothing;
    }
}

您的文本绑定变为(在XAML的相应参考资料部分中添加引用之后):

 <TextBlock Name="tbStatus" 
            Text="{Binding Value.status,
                           Converter={StaticResource statusToText}}"
            Grid.Column="0" 
            Grid.Row="2" 
            Margin="2,2,2,2" 
            FontSize="11" 
            FontWeight="Normal"></TextBlock>

我会把图像转换器留给你,因为这是基本的东西,它本质上是这段代码的重复。

请注意,这是硬编码值,这不是最佳实践。如果您计划本地化此应用程序,则应该使用枚举和资源(而不是硬编码字符串)。