如何通过按钮隐藏直到点击

时间:2014-05-07 13:31:40

标签: c# xaml visual-studio-2012 windows-phone-8

我正在创建一个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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

第一次初始加载滑块并将其值设置为零时,甚至会调用TemperatureSlider_ValueChanged事件。

每次引发事件时,都可以通过检查滑块的值来避免此行为:

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

答案 1 :(得分:0)

您的活动被触发,因为您在订阅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;
}

如果你没有向你的Slider添加Value=...属性,请检查你的xaml - 它还会触发事件并让你的按钮可见 - 这可能是主要的问题,因为你设置你的按钮是不可见的结构的结束。

请注意,对于您的情况(更改可见性),您可以通过转换器将可见性绑定到Silder的值。

相关问题