繁体   English   中英

WP7:使用转换器更改边框的背景

[英]WP7: change the background of a border with converter

我为转换器疯狂。 我知道必须在需要时使用它来更改我的值的“退出值”,但是我不知道如何为我的案子使用正确的权利。

我有简单的MVVM(仅3个字段),主窗口带有项目列表。 第一项是根据函数计算的,可以显示是或否,其他值将直接绑定。

这很好用,但是我需要根据第一个计算字段中的是或否值来更改背景和前景色。 例如:

YES (must be blue) - ITEM 1
NO (must be grey)  - ITEM 2
YES (must be blue) - ITEM 3

虽然我数据库中的内部值为(在这种情况下,计算结果是模数):

2 - ITEM 1
3 - ITEM 2
4 - ITEM 3

我的ListBox代码是这样的:

<phone:PhoneApplicationPage 
x:Class="Pasti.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="My App" Style="{StaticResource PhoneTextNormalStyle}" />
        <TextBlock Text="My List" Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

<ListBox x:Name="lstPills" Grid.Row="1" ItemsSource="{Binding AllItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" Width="440">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="90" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Border Background="HERE MUST GO THE CONVERTER, I SUPOSE">
                    <TextBlock Text="{Binding IsPair, Mode=TwoWay}"/>
                </Border>
                <TextBlock
                    Text="{Binding Name}"
                    FontSize="{StaticResource PhoneFontSizeLarge}"
                    Grid.Column="1"
                    VerticalAlignment="Center"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
</Grid>
</phone:PhoneApplicationPage>

此页面的CS代码是这样的:

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Set the page DataContext property to the ViewModel.
        this.DataContext = App.ViewModel;
    }
}

对于计算所得的字段,我将其添加到了模型中(_myNumber保存了必须检查的值):

    // Define a custom field based on some database values
    // Get is calculated, while set will force it to refresh by Notifying
    public string IsPair
    {
        get
        {
            return _myNumber % 2 == 0 ? "YES" : "NO";
        }
        set
        {
            NotifyPropertyChanged("IsPair");
        }
    }

注意:因为我不知道其他方法来强制刷新列表,所以将set属性设置为仅通知和IsPair = ""模式,当我要重新计算它时,我只是做了一个IsPair = "" 如果还有其他方法可以使用,将受到欢迎。

因此,利用此信息,如何制作一个基于IsPair值的,将Border的Background属性设置为Blue或Grey的Converter? 我看到了很多Converter示例,但仍然没有确切地做到这一点。

我想我必须在MainPage类下的MainPage.cs中放入以下内容:

// Converter for the YES-NO column on the list
public class IsPairConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (MY_CALCULATED_VALUE == "YES")
            return "Blue";

        return "Grey";
    }

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

但是,如何获取MY_CALCULATED_VALUE,以及如何在Border的Background值中设置转换器?

很近!

首先,将背景绑定到IsPair并使用转换器:

<Border Background="{Binding IsPair, Converter={StaticResource IsPairConverter}}">
    <TextBlock Text="{Binding IsPair, Mode=TwoWay}"/>
</Border>

在转换器中,根据值创建画笔:

public class IsPairConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // You might want to add additional checks for type safety
        var calculatedValue = (string)value; 

        var color = calculatedValue == "YES" ? Colors.Blue : Colors.Gray;

        return new SolidColorBrush { Color = color };
    }

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

这样就完成了。


如果只希望一次而不是每次调用IsPair都计算值,则可以在MyNumber的setter中进行MyNumber并将其分配给IsPair

private int myNumber;

public string IsPair { get; protected set; }

protected int MyNumber
{
    get
    {
        return this.myNumber;
    }

    set
    {
        this.myNumber = value;
        this.IsPair = value % 2 == 0 ? "YES" : "NO";
        this.NotifyPropertyChanged("IsPair");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM