Я новичок в разработке WPF, но я думал о том, как убить 3-х птиц одним камнем. Пример: у меня есть форма с 2 TextBox и 2 TextBlocks. Первой "птицей" было бы "обогатить" некоторый текстовый блок звездочкой, если они относятся к обязательным полям:
<TextBlock Grid.Row="0" Grid.Column="0" Text="Age" customProperty="Required" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Foot Size/>
Затем TextBlocks будет показывать свой текст по-разному, первый будет иметь звездочку, в то время как тот, у которого не определено customproperty, не будет.
Вторая птица должна иметь некоторую проверку правильности значения текстового поля, которое, если я правильно понял, было выполнено с помощью CustomValidationRule, для которого я реализовал класс:
class AgeController: ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value == null)
return new ValidationResult(false, "Null value");
int temp = 1;
Boolean noIllegalChars = int.TryParse(value.ToString(), out temp);
if (temp >= 1)
return new ValidationResult(true, null);
else
return new ValidationResult(false, "Correggi");
}
}
Добавив это в код textBlox XAML:
<TextBox.Text>
<Binding Path="blabla" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<local:AgeController ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
И это работает, но процесс проверки должен отличаться для требуемых и необязательных полей: если требуется, чтобы пустой ввод недействителен, но если он необязательный, пустое поле в порядке. Как достичь этого, не указав два разных ValidationRule, ссылаясь на текстовый блок, связанный с текстовым полем?
/TL;DR: Я пытаюсь найти способ обогатить текстовый блок атрибутом, который добавляет стиль к его тексту (звездочка или что-то, что хочет клиент), я изменяю, как обогащение изменяет текст только в одном месте) проверка правильности текстового поля, относящаяся к обогащенному текстовому блоку, будет вести себя по-разному на основе значения обогащения.
Надеюсь, я не испортил объяснения.