WP7:使用转换器更改边框的背景 [英] WP7: change the background of a border with converter

查看:80
本文介绍了WP7:使用转换器更改边框的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对转换器感到疯狂。我知道我必须在需要时使用它来更改我的值的退出值,但是我不知道如何为我的情况使用正确的权限。

I'm going crazy with converters. I know that I must use it to change the "exit value" of my values, when needed, but I don't know how to use right for my case.

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

I have my simple MVVM (3 fields only) and my main window with a list of my items. The first item is calculated depending on a function, and can show YES or NOT, the other values are binded directly.

这很好,但是我需要更改背景和前景色取决于我在第一个计算字段中具有的是或否值。例如:

This is working well, but I need to change the background and foreground colors depending on the YES or NOT value I have in the first calculated field. For example:

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

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

While the internal values in my database are (in this case the calc is modulus):

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

我的列表框代码是:

<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代码是这样的:

And the CS code is this for this page:

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

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

对于计算字段,我将此添加到了模型中(_myNumber包含我必须检查的值):

For the calculated field, I added this to the Model (_myNumber holds the value I must check):

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

NOTE: Because I don't know other way to force the list to refresh, I put the set property to only notify and the TwoWay Mode, and I just do a IsPair = "" when I want it to recalculate. If there are other way to do it, will be welcome.

因此,使用此信息,我如何制作一个基于IsPair值设置的Converter。边框为蓝色或灰色的背景属性?我看到了很多Converter示例,但仍然没有做到这一点。

So, with this info, how can I made a Converter that, based on my IsPair value, set the Background property of the Border to Blue or Grey? I saw a lot of Converter examples, but still don't get the point to do exactly this.

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

I suppose I must put something like this in the MainPage.cs, under the MainPage Class:

// 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,以及如何设置边框的背景值中的转换器?

But how to get the MY_CALCULATED_VALUE, and how to set the converter in the Background value of the Border?

推荐答案

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

First, bind the background to IsPair and use the converter:

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

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

In your converter, create a brush depending on the value:

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 的设置器中进行计算,并将其分配给 IsPair

If you want the value to be computed only one time, instead of every time IsPair is called, you can do the computation in the setter of MyNumber and assign it to 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");
    }
}

这篇关于WP7:使用转换器更改边框的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆