Как создать свойство для приложения WPF для динамически - PullRequest
2 голосов
/ 20 октября 2010

Привет всем, кому нужно динамически генерировать свойство во время выполнения, может ли кто-нибудь, кто скажет мне, как генерировать свойство в WPF во время выполнения.

в моем блоке window.resource

 <Style 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

в моем кодовом блоке окна

  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);

// класс как

 public class TextRequired : ValidationRule    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value != null)
            {
                string input = value as string;

                if (input.Length &gt; 0)
                    return new ValidationResult(true, null);
            }

            return new ValidationResult(false, "Validation error. Field input required.");
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...