2013-10-09 3 views
0

나는 새로운 winphone입니다. 바인딩 데이터에 문제가 있습니다.창 전화 동적 바인딩 데이터

나는 2 개의 속성 Temp_CTemp_F이있는 클래스 날씨가 있습니다. 나는 온도를 텍스트 블록에 묶고 싶다.

CF을 선택할 수도 있습니다.

F => 텍스트 블록 바인딩 Temp_F으로 전환하면 C => 텍스트 블록 바인딩 Temp_C으로 전환 할 때 어떻게해야합니까?

답변

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에 임시 속성과 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}" /> 
+0

예, 솔루션 2를 수행했습니다. 'public class CurrentCondition : INotifyPropertyChanged {공공 문자열 isC; 공용 문자열 temp_C {get; 세트; } 공용 문자열 temp_F {get; 세트; } 문자열 tempShow; 공용 문자열 TempShow {get { if (this.isC == true) {return temp_C + "° C"; } else { return temp_F + "° F"; } return tempShow; } 세트 { }' 하지만 '온도'를 클릭하면 자동으로 TempShow 자동 업데이트를 원합니다. 'isC'를 설정하기 위해 다시 호출 할 필요가 없습니다 –

+0

"무엇을 의미하는지 확실하지 않습니다" isC를 설정하도록 요청 "하지만 솔루션 2를 구현하는 방법에 대한 응답을 업데이트했습니다. –

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에서 온도 환경 설정을 유지해야합니다.

+0

나는 알고 있었지만 axml에서 바인딩하고 싶습니다. 그런 것. xaml : 'GridBar TextAlignment = "{Center"FontSize = "{StaticResource PhoneFontSizeExtraExtraLarge}"Grid.ColumnSpan = "2"Grid.Row = "0"x : Name = "txtTemperature"Text = "{바인딩 Temp_C, 모드 = OneWay} "/>' 모달 : public class CurrentCondition : INotifyPropertyChanged { 공용 문자열 temp_C {get; 세트; } 공용 문자열 temp_F {get; 세트; } } ' 스위치를 선택하는 방법은 C => textblock binding입니다. temp_C 스위치를 선택하는 방법은 F => textblock binding temp_F입니다. –