0

我遇到了一些应该很容易的问题。我正在为 windows phone 8 创建一个洪水警告应用程序。我有一个Severity从 JSON 提要中提取的整数,可以是 1 到 4 之间的任何值。我只是想将 int 更改为与严重性相对应的字符串,然后使用字符串通过绑定填充文本块。这是代码

namespace FloodAlertsApp
{


public class RootObject
{

    public int Severity { get; set; }
    public string AreaDescription { get; set; }
    public string Raised { get; set; }
    public string severityTxt;      

    public string getsevText()
    {

        switch (Severity)
        {
            case 1:
                severityTxt = "Severe";
                break;
            case 2:
                severityTxt = "Warning";
                break;
            case 3:
                severityTxt = "Alert";
                break;
            case 4:
                severityTxt = "";
                break;
        }
        return severityTxt;
    }
}

然后我使用绑定的xaml如下,

<phone:PhoneApplicationPage
x:Class="FloodAlertsApp.ListView"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <!--<RowDefinition Height="Auto"/>-->
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Width="Auto" HorizontalAlignment="Center"   Margin="10,10,10,10">
        <Image Width="Auto" Source="/nrw_logo_linear.png" Stretch="Fill"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="0,0,0,0">

        <ListBox Name="listBoxBeaches" SelectionChanged="listBoxBeaches_SelectionChanged" Margin="10,10,10,0" Padding="0,0,0,100" Height="{Binding ElementName=ContentPanel, Path=ActualHeight}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Grid.Row="0" x:Name="TemplateLayout" Margin="0,5,0,5">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="60" />
                        </Grid.ColumnDefinitions>

                        <StackPanel Grid.Row="0" Grid.Column="0" x:Name="Stackpnl">
                            <TextBlock Foreground="Black" FontSize="28" Margin="5,0,5,0" Text="{Binding Path=AreaDescription}"></TextBlock>
Here be the binding---->    <TextBlock Foreground="Black" FontSize="28" Margin="5,0,5,0" Text="{Binding Path=SeverityTxt}"></TextBlock>
                            <TextBlock Foreground="Black" FontSize="22" Margin="5,0,5,0" Text="Raised at "></TextBlock>
                            <TextBlock Foreground="Black" FontSize="22" Margin="5,0,5,0" Text="{Binding Path=Raised}"></TextBlock>
                        </StackPanel>

                        <Ellipse Grid.Row="0" Grid.Column="1" Margin="5,0,5,0" x:Name="StatusEllipse" Height="50" Width="50" Fill="{Binding Path = Colour}" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>


    </StackPanel>
</Grid>

</phone:PhoneApplicationPage>

其他绑定工作正常,但是当我尝试从 int 更改为字符串时,绑定显示为空。

4

2 回答 2

0

我想枚举没有问题(如果我得到你想要的):

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
   <ListView Name="TestListView">

   </ListView>
</Window>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        TestListView.ItemsSource = new[] {Severity.Severe, Severity.Alert, Severity.Warning};
    }

    enum Severity
    {
        Severe = 1,
        Warning = 2,
        Alert = 3,
        None = 4
    }
}

结果:

在此处输入图像描述

例如,您可以将简单的整数转换为枚举

TestListView.ItemsSource = new[] {1, 2, 3}.Select(i => (Severity) i);
于 2014-12-11T13:42:23.167 回答
-1

Firstly, RootObject should implement INotifyPorpertyChanged interface, so that bindings will work properly.

Having done that you should remove public field, and add a new property called severityTxt(BTW it should be called SeverityText according to the C# naming convention) with only get accessor.

The code will be like this:

public class RootObject : INotifyPropertyChanged
{
    private int serverity;

    private string areaDescription;

    private string raised;

    public int Severity 
    {
        get
        {
            return serverity;
        }
        set
        {
            serverity = value;
            NotifyPropertyChanged("Severity");
            NotifyPropertyChanged("SeverityTxt");
        }
    }

    public string AreaDescription 
    {
        get
        {
            return areaDescription;
        }
        set
        {
            areaDescription = value;
            NotifyPropertyChanged("AreaDescription");
        }
    }

    public string Raised 
    {
        get
        {
            return raised;
        }
        set
        {
            raised = value;
            NotifyPropertyChanged("Raised");
        }
    }

    public string SeverityTxt
    {
        get 
        {
            switch (Severity)
            {
                case 1:
                    return "Severe";
                case 2:
                    return "Warning";
                case 3:
                    return "Alert";
                default:
                    return string.Empty;
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

With such implementation you should bind not to severityTxt but to SeverityTxt. It's the basic solution, for more solid code you should consider using MVVM framework, such as MVVM Light and\or IValueConverter's.

于 2014-12-11T13:33:24.453 回答