簡體   English   中英

如何使按鈕不可見直到被點擊

[英]How to make by button invisible till clicked

我正在創建一個Windows 8應用程序,其中有一個滾動條和一個按鈕。 啟動頁面時,我希望在觸摸滾動條之前不可見該按鈕。 我用這一行代碼使按鈕不可見。

ContinueButton.Visibility = Visibility.Collapsed;

但是,當我進入滑塊時,更改代碼的一部分,並使按鈕的可見性可見,這是行不通的。 即使在觸摸滑塊之前,該按鈕仍可見。 我究竟做錯了什么。 以下是有關代碼的一部分。

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

當滑塊首次首次加載並且其值設置為零時,甚至會調用TemperatureSlider_ValueChanged事件。

您可以通過在每次引發事件時檢查滑塊的值來避免此行為:

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

您的事件被觸發,因為您在訂閱ValueChanged之后就更改了Slider的值:

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

如果還沒有向滑塊添加Value=...屬性,請檢查您的xaml-它也會觸發事件並使按鈕可見-這可能是主要問題,因為在按鈕結尾處將按鈕設置為不可見構造函數。

請注意,根據您的情況(更改可見性),您可以例如通過Converter將可見性綁定到Silder的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM