2014-05-07 31 views
0

我正在創建一個Windows 8應用程序,其中有一個滾動條和一個按鈕。當頁面啓動時,我希望按鈕不可見,直到觸摸滾動條。我用這行代碼隱藏了按鈕。如何使按鈕不可見直到點擊

ContinueButton.Visibility = Visibility.Collapsed; 

然而,當我進入我的代碼的滑塊改變部分和maake按鈕的知名度可見這是行不通的。即使在觸摸滑塊之前,該按鈕也變得可見。我究竟做錯了什麼。以下是有關代碼的一部分。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using Ward_Obs_Roaming.Common; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Media.Imaging; 
using Windows.UI.Xaml.Navigation; 

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236 

namespace Ward_Obs_Roaming.UI.ObsUserControls 
{ 
    public sealed partial class TemperatureObsControl : UserControl 
    { 
     public string ObsIndicatorImage 
     { 
      get 
      { 
       if (IndicatorImage.Source != null) 
        return (IndicatorImage.Source as BitmapImage).UriSource.ToString(); 
       else 
        return string.Empty; 
      } 
     } 

     public string MEWSScore 
     { 
      get; 
      set; 
     } 

     private string _Note = string.Empty; 
     public string Note 
     { 
      get { return _Note; } 
      set { _Note = value; } 
     } 

     public string TemperatureColor 
     { 
      get { return (TemperatureGrid.Background as SolidColorBrush).Color.ToString(); } 
      set 
      { 
       byte A = Byte.Parse(value.Substring(1, 2)); 
       byte R = Byte.Parse(value.Substring(3, 2)); 
       byte G = Byte.Parse(value.Substring(5, 2)); 
       byte B = Byte.Parse(value.Substring(7, 2)); 
       TemperatureGrid.Background = new SolidColorBrush(Color.FromArgb(A, R, G, B)); 
      } 
     } 

     public string TemperatureResult 
     { 
      //get { return DegreesComboBox.SelectedItem.ToString() + DecimalComboBox.SelectedItem.ToString(); } 
      //set//getting data as x.y...split and set combo box items 
      //{ 
      // string[] splitTemperature = value.Split('.'); 
      // DegreesComboBox.SelectedItem = splitTemperature[0]; 
      // DecimalComboBox.SelectedItem = "." + splitTemperature[1]; 
      //} 

      get 
      { 
       return TemperatureSlider.Value.ToString("##.#") + "°c"; 
      } 
      set 
      { 
       value = value.Replace("°c", string.Empty); 
       TemperatureSlider.Value = Double.Parse(value.ToString()); 
      } 
     } 

     public Button TemperatureContinueButton 
     { 
      get 
      { 
       return ContinueButton; 
      } 
     } 

     public TemperatureObsControl() 
     { 
      this.InitializeComponent(); 
      TemperatureSlider.ValueChanged += TemperatureSlider_ValueChanged; 
      TemperatureSlider.Value = 37; 
      ContinueButton.Content = "No Change"; 
      ContinueButton.Visibility = Visibility.Collapsed; 
     } 

     void TemperatureSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) 
     { 
      ContinueButton.Content = "Update"; 
      ContinueButton.Visibility = Visibility.Visible; 
      TemperatureTextBlock.Text = TemperatureSlider.Value.ToString("##.#") + "°c"; 

      TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.White); 
      MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.White); 
      MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.White); 

      //int bloodOxygenLevel = Int32.Parse(BloodOxygenSlider.Value.ToString()); 
      double temperature = TemperatureSlider.Value; 

      switch (CommonVariables.ObsCategory) 
      { 
       case ObsCategories.NormalObs: 
        { 
         //if (CommonVariables.is_Abnormal && CommonVariables.abnormal.temperature != null) 
         //{ 
         // if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(0).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["RedObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/exclamationMark.png")); 
         //  MEWSScore = "3"; 
         // } 
         // else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(1).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = null; 
         //  MEWSScore = "2"; 
         // } 
         // else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(2).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = null; 
         //  MEWSScore = "1"; 
         //  TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
         //  MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
         //  MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
         // } 
         // else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(3).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
         //  MEWSScore = "0"; 
         // } 
         // else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(4).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
         //  MEWSScore = "0"; 
         // } 
         // else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(5).Split(';').ElementAt<string>(1))) 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = null; 
         //  MEWSScore = "1"; 
         //  TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
         //  MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
         //  MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
         // } 
         // else 
         // { 
         //  TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
         //  IndicatorImage.Source = null; 
         //  MEWSScore = "2"; 
         // } 
         //} 
         if (CommonVariables.is_Abnormal && CommonVariables.abnormal.temperature != null) 
         { 
          if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(0).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["RedObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/exclamationMark.png")); 
           MEWSScore = "3"; 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(1).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(2).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(3).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
           MEWSScore = "0"; 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(4).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
         } 
         else 
         { 

          if (temperature <= 34)//<=34 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["RedObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/exclamationMark.png")); 
           MEWSScore = "3"; 
          } 
          else if (temperature <= 35)//34-35 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
          else if (temperature <= 36)//35-36 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else if (temperature <= 37)//36-37 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
           MEWSScore = "0"; 
          } 
          else if (temperature <= 38)//37-38 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
           MEWSScore = "0"; 
          } 
          else if (temperature <= 39)//38-39 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else//>39 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
         } 
        } 
        break; 
       case ObsCategories.NeurologicalObs: 
        { 
         if (CommonVariables.is_Abnormal && CommonVariables.abnormal.temperature != null) 
         { 
          if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(0).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(1).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(2).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
           MEWSScore = "0"; 
          } 
          else if (temperature <= double.Parse(CommonVariables.abnormal.temperature.ElementAt<string>(3).Split(';').ElementAt<string>(1))) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
         } 
         else 
         { 

          if (temperature <= 35) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
          else if (temperature <= 35.9 || (temperature <= 38.5 && temperature >= 38.1)) 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["YellowObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "1"; 
           TemperatureTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreLabel.Foreground = new SolidColorBrush(Colors.Black); 
           MEWSScoreTextBlock.Foreground = new SolidColorBrush(Colors.Black); 
          } 
          else if (temperature <= 38)//36-38 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["GreenObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = new BitmapImage(new Uri("ms-appx:///Images/tickMark.png")); 
           MEWSScore = "0"; 
          } 
          else//>=38.6 
          { 
           TemperatureGrid.Background = (Application.Current as App).Resources["AmberObsBrush"] as SolidColorBrush; 
           IndicatorImage.Source = null; 
           MEWSScore = "2"; 
          } 
         } 
        } 
        break; 
      } 
      MEWSScoreTextBlock.Text = MEWSScore; 
     } 

     //private void DegreesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     //{ 
     // ContinueButton.Content = "Update"; 

     // DegreesTextBlock.Text = DegreesComboBox.SelectedItem.ToString(); 
     // int selectedDegrees = Int32.Parse(DegreesComboBox.SelectedItem.ToString()); 

     // if (selectedDegrees < 30) //<30 
     //  BindedTemperatureSP.Background = new SolidColorBrush(Colors.LightGreen); 
     // else if (selectedDegrees < 70)//30 to 70 
     //  BindedTemperatureSP.Background = new SolidColorBrush(Colors.Green); 
     // else //>70 
     //  BindedTemperatureSP.Background = new SolidColorBrush(Colors.Red); 
     //} 

     //private void DecimalComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     //{ 
     // ContinueButton.Content = "Update"; 
     // DecimalTextBlock.Text = DecimalComboBox.SelectedItem.ToString(); 

     //} 

     private void MinusButton_Click(object sender, RoutedEventArgs e) 
     { 
      TemperatureSlider.Value -= TemperatureSlider.StepFrequency; 
     } 

     private void PlusButton_Click(object sender, RoutedEventArgs e) 
     { 
      TemperatureSlider.Value += TemperatureSlider.StepFrequency; 
     } 
    } 
} 
+0

這只是一個想法,但它可能是由於該屬性的值在控件實際加載到屏幕上之前發生更改,從而觸發事件。而不是在INIT部分(TemperatureOrbsControl())中設置事件處理程序,你可能想把它放在不同的地方,如渲染事件或其他東西。 – MaxOvrdrv

回答

0

TemperatureSlider_ValueChanged事件當滑塊得到了最初加載的第一次,其價值得到了設置爲零甚至被稱爲。

您可以通過每個引發該事件時檢查滑塊的值避免這種情況:

void TemperatureSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) 
{ 
    if (TemperatureSlider.Value != 0) 
    { 
     ContinueButton.Visibility = Visibility.Visible; 
    } 
} 
0

你的事件被激發,因爲你只是訂閱ValueChanged後更改滑塊值:

public TemperatureObsControl() 
{ 
    this.InitializeComponent(); 
    TemperatureSlider.ValueChanged += TemperatureSlider_ValueChanged; // here you subscribe 
    TemperatureSlider.Value = 37; // here you change 
    ContinueButton.Content = "No Change"; 
    ContinueButton.Visibility = Visibility.Collapsed; 
} 

檢查你的xaml如果你沒有添加Value=...屬性到你的滑塊 - 它也會觸發事件並使你的按鈕可見 - 這可能是主要的問題,因爲你設置你的按鈕隱藏在構造函數。

請注意,針對您的情況(更改可視性),您可以通過Converter將Binder Visibility綁定到Silder的值。