How to Generate Property for WPF Application for dynamically
Hi everyone I need to generate property dynamically at run time can any body tell me how to generate property in WPF at run time.
in my window.resource block
<Styl开发者_如何转开发e x:Key="EditableTextBox" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="#FFF8E48F" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="LightCoral"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style
in my window code block
TextBox txt = new TextBox();
txt.Name = "txt1";
txt.Width = 200;
txt.Style = (Style)this.FindResource("EditableTextBox");
TextRequired txtreq = new TextRequired();
Binding txtbind = new Binding("MyText"); // name of the property
stp.Children.Add(txt);
txtbind.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
txtbind.Mode = BindingMode.OneWayToSource;
txtbind.ValidationRules.Add(txtreq);
BindingOperations.SetBinding(txt, TextBox.TextProperty, txtbind);
//a class as
public class TextRequired : ValidationRule {
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value != null)
{
string input = value as string;
if (input.Length > 0)
return new ValidationResult(true, null);
}
return new ValidationResult(false, "Validation error. Field input required.");
}
}
精彩评论