I have a User Control that takes input from user. This user control informs the user whether input value within the allowable range.
In the main Window, I will use several of this User Control to get values of different parameters from the User.
At the end, there will be a submit button.
Now, I want to disable the button, if any one of the input values in the user control is out of range. How do I do that? I am stuck here.
If this is the wrong way to do it, please do correct me as well. I would love to follow a best practice method.
Many many thanks in advance.
XAML Code for User Control "UserInputFieldUC":
<UserControl x:Class="TestUserInput.UserInputFieldUC"
...
xmlns:local="clr-namespace:TestUserInput"
x:Name="parent">
<StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=parent}" >
<TextBlock Name="UserLabel" Width="50" Text="{Binding Path=Label}"/>
<TextBox Name="MetricValue" Width="50" Text="{Binding Path=Value}" TextChanged="MetricValue_TextChanged"/>
<TextBlock Name="MetricUnits" Width="50" Text="{Binding Path=Units}" VerticalAlignment="Center"/>
<TextBlock Name="ErrorDisplay" Width="50" VerticalAlignment="Center"/>
</StackPanel>
</UserControl>
Code-behind for "UserInputFieldUC":
namespace TestUserInput
{
/// <summary>
/// Interaction logic for UserInputFieldUC.xaml
/// </summary>
public partial class UserInputFieldUC : UserControl
{
public UserInputFieldUC()
{
InitializeComponent();
}
#region Label DP
/// <summary>
/// Label dependency property
/// </summary>
public static readonly DependencyProperty LPShowFieldUCPercentCheck =
DependencyProperty.Register("Label",
typeof(string),
typeof(UserInputFieldUC),
new PropertyMetadata(""));
/// <summary>
/// Gets or sets the Label which is displayed to the field
/// </summary>
public string Label
{
get { return GetValue(LPShowFieldUCPercentCheck) as String; }
set { SetValue(LPShowFieldUCPercentCheck, value); }
}
#endregion // Label DP
#region Value DP
/// <summary>
/// Value dependency property.
/// </summary>
public static readonly DependencyProperty ValueProp =
DependencyProperty.Register("Value",
typeof(string),
typeof(UserInputFieldUC),
new PropertyMetadata(""));
/// <summary>
/// Gets or sets the value being displayed
/// </summary>
public string Value
{
get { return GetValue(ValueProp) as String; }
set { SetValue(ValueProp, value); }
}
#endregion // Value DP
#region Units DP
/// <summary>
/// Units dependency property
/// </summary>
public static readonly DependencyProperty UnitsProperty =
DependencyProperty.Register("Units",
typeof(string),
typeof(UserInputFieldUC),
new PropertyMetadata(""));
/// <summary>
/// Gets or sets the Units which is displayed to the field
/// </summary>
public string Units
{
get { return GetValue(UnitsProperty) as String; }
set { SetValue(UnitsProperty, value); }
}
#endregion // Units DP
#region Maximum Allowable Input Value DP
public static readonly DependencyProperty MaxInputProperty =
DependencyProperty.Register("UpperLimit",
typeof(string),
typeof(UserInputFieldUC), new PropertyMetadata(""));
public string UpperLimit
{
get { return GetValue(MaxInputProperty) as String; }
set { SetValue(MaxInputProperty, value); }
}
#endregion // Max Value DP
#region Minimum Allowable Input DP
public static readonly DependencyProperty MinInputProperty =
DependencyProperty.Register("LowerLimit",
typeof(string),
typeof(UserInputFieldUC), new PropertyMetadata(""));
public string LowerLimit
{
get { return GetValue(MinInputProperty) as String; }
set { SetValue(MinInputProperty, value); }
}
#endregion // Max Value DP
#region Display Error DP
public static readonly DependencyProperty ErrorProperty =
DependencyProperty.Register("ShowErr",
typeof(string),
typeof(UserInputFieldUC),
new PropertyMetadata(""));
public string ShowErr
{
get { return GetValue(ErrorProperty) as String; }
set { SetValue(ErrorProperty, value); }
}
#endregion // Display Error DP
/// <summary>
/// Check user input
/// </summary>
private void MetricValue_TextChanged(object sender, TextChangedEventArgs e)
{
string inputText = MetricValue.Text;
if (inputText == "")
inputText = "0";
double inputValue = Convert.ToDouble(inputText);
double maxValue = Convert.ToDouble(UpperLimit);
double minValue = Convert.ToDouble(LowerLimit);
ErrorDisplay.Text = "OK";
if (inputValue <= minValue)
{
ErrorDisplay.Text = "Err";
}
if (inputValue >= maxValue)
{
ErrorDisplay.Text = "Err";
}
}
}
}
XAML code for "MainWindow" displaying User Controls and submit button:
<Window x:Class="TestUserInput.MainWindow"
...
xmlns:local="clr-namespace:TestUserInput"
Title="MainWindow" Height="450" Width="350">
<Grid>
<StackPanel Margin="5">
<local:UserInputFieldUC Margin="5" Label="Param1" Value="{Binding Path=Param1}" Units="m" LowerLimit="5" UpperLimit="10"/>
<local:UserInputFieldUC Margin="5" Label="Param2" Value="{Binding Path=Param2}" Units="m" LowerLimit="50" UpperLimit="100"/>
<Button Content="Submit"/>
</StackPanel>
</Grid>
</Window>
and the viewmodel for the main Window implements the usual INotifyPropertyChangedview interface
public class MainWindowViewModel : INotifyPropertyChanged
{
public double Param1 { get; set; }
public double Param2 { get; set; }
...
...
}