我是新的winphone。我有一個綁定數據的問題。窗口手機動態綁定數據
我有一個班的天氣有2個屬性Temp_C
和Temp_F
。我想將溫度綁定到文本塊。
我也有一個開關選擇C
和F
。
當我切換到C =>文本塊綁定Temp_C
當我切換到F =>文本塊綁定Temp_F
時,我該怎麼做。
我是新的winphone。我有一個綁定數據的問題。窗口手機動態綁定數據
我有一個班的天氣有2個屬性Temp_C
和Temp_F
。我想將溫度綁定到文本塊。
我也有一個開關選擇C
和F
。
當我切換到C =>文本塊綁定Temp_C
當我切換到F =>文本塊綁定Temp_F
時,我該怎麼做。
一個解決方案可能是剛剛有兩個文本塊,然後只需撥動有知名度:
<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" .. />
還是另有其他的解決方案只是在你的視圖模型中添加臨時屬性和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}" />
的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中保留溫度偏好。
我知道,但我想從axml結合。有些事情是這樣的。 xaml: '
是的,我做的第二溶液。 '公共類CurrentCondition:INotifyPropertyChanged {公共字符串isC; public string temp_C {get;組; } public string temp_F {get;組; } string tempShow; public string TempShow {get {this.isC == true} {return {temp_C +「°C」; } else { return temp_F +「°F」; } return tempShow; }設置{ }}' 但是我想TempShow自動更新,當我點擊不需要重新調用設置'isC'的'temperatureTogled' –
不知道你的意思是「呼叫設置isC「,但我更新了我的反應,我將如何實施解決方案2 –