亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當 WPF 應用程序中的文本框輸入更改時,如何更改主窗口顏色?

當 WPF 應用程序中的文本框輸入更改時,如何更改主窗口顏色?

C#
夢里花落0921 2022-01-09 15:40:43
當 WPF 應用程序中的文本框輸入更改時,我想更改主窗口顏色。目前,當鼠標懸停或進入文本框顏色會發生變化,但我會刪除它,因為我不想要這種效果。但是當鼠標光標進入文本框時我想要同樣的效果,它應該改變主要的背景顏色。MainWindow 背景顏色為:LightBlue,當鼠標光標進入A輸入框時LightGreen,當鼠標光標進入B輸入框時淺灰色,所有其他情況。模型.cspublic abstract class ObservableBase : INotifyPropertyChanged{    public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")    {        if (!EqualityComparer<TValue>.Default.Equals(field, default(TValue)) && field.Equals(newValue)) return;        field = newValue;        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));    }    public event PropertyChangedEventHandler PropertyChanged;    public void NotifyPropertyChanged(string propertyName)    {        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));    }}public abstract class ViewModelBase : ObservableBase{    public bool IsInDesignMode        => (bool)DesignerProperties.IsInDesignModeProperty            .GetMetadata(typeof(DependencyObject))            .DefaultValue;}
查看完整描述

2 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

我修改了你的源代碼:


<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApplication1"

        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">


<Window.DataContext>

    <local:MainViewModel/>

</Window.DataContext>

<Window.Resources>

    <local:BoolToBackgroundColorConverter x:Key="BoolToBackgroundColorConverter"/>

</Window.Resources>


<Grid Background="{Binding BackgroundColor}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto"/>

            <ColumnDefinition Width="Auto"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

        </Grid.RowDefinitions>

        <Grid.Resources>

            <SolidColorBrush x:Key="LightGreen" Color="LightGreen" />

            <SolidColorBrush x:Key="LightBlue" Color="LightBlue" />

            <SolidColorBrush x:Key="LightGray" Color="LightGray" />

            <SolidColorBrush x:Key="White" Color="White" />

            <Style TargetType="TextBlock">

                <Setter Property="VerticalAlignment" Value="Center"/>

            </Style>

            <Style TargetType="TextBox">

                <Setter Property="VerticalContentAlignment" Value="Center"/>

                <Setter Property="Margin" Value="10"/>

                <Setter Property="Width" Value="100"/>

                <Setter Property="Height" Value="25"/>

                <Setter Property="Grid.Column" Value="1"/>

                <Style.Triggers>

                    <Trigger Property="IsMouseOver" Value="True">

                    </Trigger>

                </Style.Triggers>

                <!--<Setter Property="Background" Value="{StaticResource White}" />-->

            </Style>                

        </Grid.Resources>


        <TextBlock Text="Value A"/>

        <TextBox Text="{Binding ValueA, UpdateSourceTrigger=PropertyChanged}">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="MouseEnter" >

                    <i:InvokeCommandAction Command="{Binding MouseEnterCommand}" CommandParameter="{StaticResource LightBlue}" />

                </i:EventTrigger>

                <i:EventTrigger EventName="MouseLeave" >

                    <i:InvokeCommandAction Command="{Binding MouseLeaveCommand}" CommandParameter="{StaticResource White}" />

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </TextBox>


        <TextBlock Text="Value B" Grid.Row="1"/>

        <TextBox Text="{Binding ValueB, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="MouseEnter" >

                    <i:InvokeCommandAction Command="{Binding MouseEnterCommand}" CommandParameter="{StaticResource LightGreen}" />

                </i:EventTrigger>

                <i:EventTrigger EventName="MouseLeave" >

                    <i:InvokeCommandAction Command="{Binding MouseLeaveCommand}" CommandParameter="{StaticResource White}" />

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </TextBox>


        <TextBlock Text="Value C" Grid.Row="2"/>

        <TextBox Text="{Binding ValueC}"                 Grid.Row="2">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="MouseEnter" >

                    <i:InvokeCommandAction Command="{Binding MouseEnterCommand}" CommandParameter="{StaticResource LightGray}" />

                </i:EventTrigger>

                <i:EventTrigger EventName="MouseLeave" >

                    <i:InvokeCommandAction Command="{Binding MouseLeaveCommand}" CommandParameter="{StaticResource White}" />

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </TextBox>


        <TextBlock Text="Value D" Grid.Row="3"/>

        <TextBox Text="{Binding ValueD}"                 Grid.Row="3">

            <i:Interaction.Triggers>

                <i:EventTrigger EventName="MouseEnter" >

                    <i:InvokeCommandAction Command="{Binding MouseEnterCommand}" CommandParameter="{StaticResource LightGray}" />

                </i:EventTrigger>

                <i:EventTrigger EventName="MouseLeave" >

                    <i:InvokeCommandAction Command="{Binding MouseLeaveCommand}" CommandParameter="{StaticResource White}" />

                </i:EventTrigger>

            </i:Interaction.Triggers>

        </TextBox>


    </Grid>

</Grid>

public class MainViewModel : ViewModelBase

    {

        public ICommand MouseEnterCommand { get; set; }

        public ICommand MouseLeaveCommand { get; set; }

        public MainViewModel()

        {

            valueAisValid = true;

            valueBisValid = true;

            MouseEnterCommand = new RelayCommand<object>(MouseEnterCommandHandler);

            MouseLeaveCommand =new  RelayCommand<object>(MouseLeaveCommandHandler);

            if (IsInDesignMode)

            {

                Calc();

            }

        }


        #region Properties


        private string _backgroundColor;

        public string BackgroundColor

        {

            get { return _backgroundColor; }

            set { _backgroundColor = value; NotifyPropertyChanged(nameof(BackgroundColor)); }

        }



        private string valueA;

        public string ValueA

        {

            get { return valueA; }

            set

            {

                if (!string.IsNullOrEmpty(value))

                {

                    double d;

                    Set(ref valueA, value);

                    Set(ref valueAisValid, double.TryParse(ValueA, out d));

                    NotifyPropertyChanged(nameof(ValueAIsValid));

                    Calc();

                }

            }

        }


        private bool valueAisValid;

        public bool ValueAIsValid => valueAisValid;


        private string valueB;

        public string ValueB

        {

            get { return valueB; }

            set

            {

                if (!string.IsNullOrEmpty(value))

                {

                    double d;

                    Set(ref valueB, value);

                    Set(ref valueBisValid, double.TryParse(ValueB, out d));

                    NotifyPropertyChanged(nameof(ValueBIsValid));

                    Calc();

                }

            }

        }


        private bool valueBisValid;

        public bool ValueBIsValid => valueBisValid;


        private string valueC;

        public string ValueC

        {

            get { return valueC; }

            set { Set(ref valueC, value); }

        }


        private string valueD;

        public string ValueD

        {

            get { return valueD; }

            set { Set(ref valueD, value); }

        }


        public bool InputsValid => ValueAIsValid && ValueBIsValid;


        #endregion


        #region Methods


        private void Calc()

        {

            if (InputsValid)

            {

                double sum = Convert.ToDouble(valueA) + Convert.ToDouble(valueB);

                double product = Convert.ToDouble(valueA) * Convert.ToDouble(valueB);

                ValueC = sum.ToString(CultureInfo.InvariantCulture);

                ValueD = product.ToString(CultureInfo.InvariantCulture);

            }

            else

            {

                ValueC = "NAN";

                ValueD = "NAN";

            }

        }


        private void MouseEnterCommandHandler(object parameter)

        {

            if (parameter != null)

            {

                BackgroundColor = parameter.ToString();

            }

        }


        private void MouseLeaveCommandHandler(object parameter)

        {

            if (parameter !=null)

            {

                BackgroundColor = parameter.ToString();

            }

        }


        #endregion

    }

請將 System.Windows.Interactivity dll 添加到項目中并創建一個繼承 ICommand 接口的 RelayCommand 類。


查看完整回答
反對 回復 2022-01-09
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

這是 MainWindow.xaml.cs


using System.Windows;

using System.Windows.Input;

using System.Windows.Media;


namespace WpfApp1

{

    public partial class MainWindow

    {

        public MainWindow()

        {

            InitializeComponent();

            AttachCustomBehaviors();

        }


        // There's a better way of doing this, read attached behavior in wpf

        private void AttachCustomBehaviors()

        {

            TextBoxA.GotFocus += (s, args) => ChangeMainWindowBackground(this, Brushes.LightBlue);

            TextBoxB.GotFocus += (s, args) => ChangeMainWindowBackground(this, Brushes.LightGreen);

            TextBoxA.LostFocus += (s, args) => ChangeMainWindowBackground(this, Brushes.Gray);

            TextBoxB.LostFocus += (s, args) => ChangeMainWindowBackground(this, Brushes.Gray);

        }


        private void InputFieldsHovered(object sender, MouseEventArgs e)

        {

            if (!TextBoxA.IsFocused && !TextBoxB.IsFocused)

            {

                ChangeMainWindowBackground(this, Brushes.Gray);

            }

            else if(TextBoxA.IsFocused)

            {

                ChangeMainWindowBackground(this, Brushes.LightBlue);

            }

            else if (TextBoxB.IsFocused)

            {

                ChangeMainWindowBackground(this, Brushes.LightGreen);

            }

        }


        private static void ChangeMainWindowBackground(Window window, SolidColorBrush color)

        {

            window.Background = color;

        }

    }

}

這是更新的 xaml:


<Window x:Class="WpfApp1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WpfApp1"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800"

        Background="Gray">

    <Window.DataContext>

        <local:MainViewModel/>

    </Window.DataContext>


    <Window.Resources>

        <local:BoolToBackgroundColorConverter x:Key="BoolToBackgroundColorConverter"/>

    </Window.Resources>


    <Grid x:Name="MainGrid" HorizontalAlignment="Center" VerticalAlignment="Center">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="Auto"/>

            <ColumnDefinition Width="Auto"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

        </Grid.RowDefinitions>

        <Grid.Resources>

            <SolidColorBrush x:Key="White" Color="White" />

            <Style TargetType="TextBlock">

                <Setter Property="VerticalAlignment" Value="Center"/>

            </Style>

            <Style TargetType="TextBox" x:Key="TextBox">

                <Setter Property="VerticalContentAlignment" Value="Center"/>

                <Setter Property="Margin" Value="10"/>

                <Setter Property="Width" Value="100"/>

                <Setter Property="Height" Value="25"/>

                <Setter Property="Grid.Column" Value="1"/>

            </Style>

            <Style TargetType="TextBox" x:Key="TextBoxA" BasedOn="{StaticResource TextBox}">

                <Setter Property="Background" Value="{Binding ValueAIsValid, Converter={StaticResource BoolToBackgroundColorConverter}, ConverterParameter={StaticResource White}}" />

            </Style>

            <Style TargetType="TextBox" x:Key="TextBoxB" BasedOn="{StaticResource TextBox}">

                <Setter Property="Background" Value="{Binding ValueBIsValid, Converter={StaticResource BoolToBackgroundColorConverter}, ConverterParameter={StaticResource White}}" />

            </Style>

            <Style TargetType="TextBox" BasedOn="{StaticResource TextBox}"/>

        </Grid.Resources>


        <TextBlock Text="Value A"/>

        <TextBox x:Name="TextBoxA" Text="{Binding ValueA, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxA}" MouseEnter="InputFieldsHovered"/>


        <TextBlock Text="Value B" Grid.Row="1"/>

        <TextBox x:Name="TextBoxB" Text="{Binding ValueB, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxB}" Grid.Row="1" MouseEnter="InputFieldsHovered"/>


        <TextBlock Text="Value C" Grid.Row="2"/>

        <TextBox x:Name="TextBoxC" Text="{Binding ValueC}" IsReadOnly="True" Background="Gray" Grid.Row="2" MouseEnter="InputFieldsHovered"/>


        <TextBlock Text="Value D" Grid.Row="3"/>

        <TextBox x:Name="TextBoxD" Text="{Binding ValueD}" Background="Gray" IsReadOnly="True" Grid.Row="3" MouseEnter="InputFieldsHovered"/>

    </Grid>

</Window>

你的 ViewModel.cs


using System;

using System.Globalization;


namespace WpfApp1

{

    public class MainViewModel : ViewModelBase

    {

        public MainViewModel()

        {

            valueAisValid = true;

            valueBisValid = true;

        }


        #region Properties


        private string valueA;

        public string ValueA

        {

            get => valueA;

            set

            {

                Set(ref valueA, value);

                Set(ref valueAisValid, double.TryParse(ValueA, out double d));

                NotifyPropertyChanged(nameof(ValueAIsValid));

                Calc();

            }

        }


        private bool valueAisValid;

        public bool ValueAIsValid => valueAisValid;


        private string valueB;

        public string ValueB

        {

            get => valueB;

            set

            {

                Set(ref valueB, value);

                Set(ref valueBisValid, double.TryParse(ValueB, out double d));

                NotifyPropertyChanged(nameof(ValueBIsValid));

                Calc();

            }

        }


        private bool valueBisValid;

        public bool ValueBIsValid => valueBisValid;


        private string valueC;

        public string ValueC

        {

            get => valueC;

            set => Set(ref valueC, value);

        }


        private string valueD;

        public string ValueD

        {

            get => valueD;

            set => Set(ref valueD, value);

        }


        public bool InputsValid => ValueAIsValid && ValueBIsValid;


        #endregion


        #region Methods


        private void Calc()

        {

            if (InputsValid)

            {

                double sum = Convert.ToDouble(valueA) + Convert.ToDouble(valueB);

                double product = Convert.ToDouble(valueA) * Convert.ToDouble(valueB);

                ValueC = sum.ToString(CultureInfo.InvariantCulture);

                ValueD = product.ToString(CultureInfo.InvariantCulture);

            }

            else

            {

                ValueC = "NAN";

                ValueD = "NAN";

            }

        }


        #endregion

    }

}


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 267 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號