-
May 25th, 2016, 12:32 AM
#1
c# conditional formating
Hi
how can i format a textbox in c# ,precisely let say if the textbox value is between 100 & 200 the background color will change .
i appreciate the help
My regards
-
May 25th, 2016, 10:04 AM
#2
Re: c# conditional formating
Look at the BackColor property of the Textbox class in Msdn
-
May 26th, 2016, 04:33 PM
#3
Re: c# conditional formating
I would bind the Text to a property and bind the Background to a property
Code:
<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"
mc:Ignorable="d"
Title="MainWindow" Height="80" Width="200">
<Grid>
<StackPanel Orientation="Horizontal" >
<TextBox x:Name="MyTextBox" HorizontalAlignment="Left" VerticalAlignment="Center" MinWidth="100" Margin="10,10,10,10"
Text="{Binding Path=TextVal, Mode=TwoWay}"
Background="{Binding Path=TextBackColor, Mode=TwoWay}">
</TextBox>
<Button Content="Dummy" HorizontalAlignment="Left" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Window>
then in the code behind
Code:
using System;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private String _textVal;
public event PropertyChangedEventHandler PropertyChanged;
private Brush _textBackColor;
public Brush TextBackColor
{
get { return _textBackColor; }
set
{
_textBackColor = value;
OnPropertyChanged();
}
}
public String TextVal
{
get { return _textVal; }
set
{
_textVal = value;
int num;
if (int.TryParse(value, out num) && (num > 99) && (num < 201))
{
TextBackColor = Brushes.LightBlue;
}
else
{
TextBackColor = Brushes.Orange;
}
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|