Windows phone 7 窗口电话动态绑定数据

Windows phone 7 窗口电话动态绑定数据,windows-phone-7,binding,Windows Phone 7,Binding,我是新来的winphone。我对绑定数据有问题 我有一个带有两个属性的类天气Temp\u C,和Temp\u F。我想将温度绑定到文本块 我还有一个开关可以选择C和F 当我切换到C=>text block bindingTemp_C时,当我切换到F=>text block bindingTemp_F时,我该怎么做 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Grid.ColumnDe

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

我有一个带有两个属性的类天气
Temp\u C
,和
Temp\u F
。我想将温度绑定到文本块

我还有一个开关可以选择
C
F

当我切换到C=>text block binding
Temp_C
时,当我切换到F=>text block binding
Temp_F
时,我该怎么做

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

在发布应用程序中,您可能应该使用WP工具包中的切换控件。您还应该在IsolatedStorage DB中保留温度首选项

一个解决方案可以是只有两个文本块,并在其中切换可见性:

<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中添加一个临时属性和一个IsCelsus属性,在临时属性中的两个值之间切换并绑定临时属性:

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}" />
公共类当前条件:INotifyPropertyChanged
{
私立学校;
公共学校
{
获取{return isC;}
设置
{
如果(isC!=值)
{
isC=值;
本.RaisePropertyChanged(“IsC”);
本.RaisePropertyChanged(“TempShow”);
}
}
}
私有字符串temp_C;
公共字符串临时值
{
获取{return temp_C;}
设置
{
如果(温度C!=值)
{
温度C=数值;
本.RaisePropertyChanged(“Temp_C”);
本.RaisePropertyChanged(“TempShow”);
}
}
}
私有字符串温度;
公共字符串临时值
{
获取{return temp_F;}
设置
{
如果(温度F!=值)
{
温度F=数值;
本.RaisePropertyChanged(“Temp_F”);
本.RaisePropertyChanged(“TempShow”);
}
}
}
私人字符串显示;
公共字符串临时显示
{
得到
{
如果(this.isC==true)
{
返回温度C+“°C”;
}
其他的
{
返回温度F+“°F”;
}
回归演出;
}
}
}

我知道这一点,但我想从axml绑定。诸如此类的事。xaml:
模式:
公共类当前条件:INotifyPropertyChanged{public string temp_C{get;set;}公共字符串temp_F{get;set;}
我该怎么做,choose开关是C=>textblock binding temp_C choose开关是F=>textblock binding temp_FYes,我做了第二个解决方案<代码>公共类当前条件:INotifyPropertyChanged{public string isC;public string temp_C{get;set;}public string temp_F{get;set;}string tempShow;public string tempShow{get{if(this.isC==true){返回temp_C+“°C”;}else{return temp_F+“°F”}return tempShow;}set{}但是当我点击
temperatureTogled
时,我想要tempShow自动更新,它不需要重新调用来设置
isC
不确定您所说的“不需要重新调用来设置isC”是什么意思但我更新了我的回答,说明了我将如何实施解决方案2
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}" />