我遇到了一些应该很容易的问题。我正在为 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 更改为字符串时,绑定显示为空。