窗口手机动态绑定数据

时间:2013-10-09 15:19:17

标签: windows-phone-7 binding

我是新的winphone。我有绑定数据的问题。

我有一个包含2个属性Temp_CTemp_F的课程天气。我想将温度绑定到文本块。

我也可以选择CF

当我切换到C =>时该怎么办?当我切换到F =>时,文本块绑定Temp_C文本块绑定Temp_F

2 个答案:

答案 0 :(得分:0)

Xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock TextAlignment="Center" FontSize="{StaticResource PhoneFontSizeExtraExtraLarge}" Grid.ColumnSpan="2" Grid.Row="0" x:Name="txtTemperature" />

        <RadioButton Checked="CentTempChecked" GroupName="Temperature" Grid.Column="0" Grid.Row="1" Content="C" />
        <RadioButton Checked="FarTempChecked" GroupName="Temperature" Grid.Column="1" Grid.Row="1" Content="F" />
    </Grid>

背后的代码

using System.Globalization;

public partial class MainPage : PhoneApplicationPage
{
    private TemperatureUnitType temperatureUnitType = TemperatureUnitType.Celsius;

    public MainPage()
    {
        InitializeComponent();

        BindTemperature();
    }

    private void CentTempChecked(object sender, RoutedEventArgs e)
    {
        this.temperatureUnitType = TemperatureUnitType.Celsius;

        BindTemperature();
    }

    private void FarTempChecked(object sender, RoutedEventArgs e)
    {
        this.temperatureUnitType = TemperatureUnitType.Fahrenheit;

        BindTemperature();
    }

    private void BindTemperature()
    {
        var temperature = new Temperature();

        txtTemperature.Text = 
            this.temperatureUnitType == TemperatureUnitType.Celsius ? 
            temperature.Celsius.ToString(CultureInfo.InvariantCulture) : temperature.Fahrenheit.ToString(CultureInfo.InvariantCulture);
    }
}

public enum TemperatureUnitType
{
    Celsius = 0,
    Fahrenheit = 1,
}

public class Temperature
{
    public double Celsius { get { return 45.3d; } }
    public double Fahrenheit { get { return 96.3d; } }
}

在出版物应用程序中,您应该使用WP工具包中的切换控件。您还应该在IsolatedStorage DB中保持温度偏好。

答案 1 :(得分:0)

解决方案可能是只有两个文本块,只需切换可见性:

<TextBlock Text="{Binding Temp_C}" Visibility="{Binding Checked,ElementName=temperatureTogled,Converter={StaticResource boolToVisibilityConverter}}"/>
    <TextBlock Text="{Binding Temp_F}" Visibility="{Binding Checked,ElementName=temperatureTogled,Converter={StaticResource boolToNotVisibilityConverter}}"/>

    <toolkit:ToggleSwitch  x:Name="temperatureTogled" .. />

或者另一种解决方案只是在viewModel中添加Temp属性和IsCelsus属性,在Temp属性中的两个值之间切换并绑定Temp属性:

public class CurrentCondition : INotifyPropertyChanged
{
    private bool isC;

    public bool IsC
    {
        get { return isC; }
        set
        {
            if (isC != value)
            {
                isC = value;
                this.RaisePropertyChanged("IsC");
                this.RaisePropertyChanged("TempShow");
            }
        }
    }

    private string temp_C;

    public string Temp_C
    {
        get { return temp_C; }
        set
        {
            if (temp_C != value)
            {
                temp_C = value;
                this.RaisePropertyChanged("Temp_C");
                this.RaisePropertyChanged("TempShow");
            }
        }
    }

    private string temp_F;

    public string Temp_F
    {
        get { return temp_F; }
        set
        {
            if (temp_F != value)
            {
                temp_F = value;
                this.RaisePropertyChanged("Temp_F");
                this.RaisePropertyChanged("TempShow");
            }
        }
    }

    private string tempShow;

    public string TempShow
    {
        get
        {
            if (this.isC == true)
            {
                return temp_C + "°C";
            }
            else
            {
                return temp_F + "°F";
            }
            return tempShow;
        }
    }
}


 <TextBlock Text="{Binding TempShow}"/>
<toolkit:ToggleSwitch  x:Name="temperatureTogled" Checked="{Binding IsC,Mode=TwoWay}" />
相关问题