我有一個ItemsControl來顯示翻譯文本字段。 我想設置驗證,所以如果所有的翻譯都是空的,就會出現錯誤,並且字段被標記爲「錯誤」。 有沒有可能做到這一點?在wpf驗證ItemsControl不起作用
我的XAML:
<ItemsControl x:Name="LanguageItemsControl" ItemsSource="{Binding Path=Translations, Mode=TwoWay}"
LostFocus="OnLostFocus" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="5,2,5,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="47*"/>
<ColumnDefinition Width="53*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="ItemLabel" VerticalAlignment="Center"
Text="{Binding Path=Key, StringFormat={x:Static res:Resources.lblCaption}}" />
<TextBox Grid.Column="1" x:Name="ItemText" VerticalAlignment="Center"
HorizontalAlignment="Stretch" Margin="2,0,22,0"
Text="{Binding Path=Value, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, NotifyOnValidationError=true}"
LostFocus="OnLostFocus"
AcceptsReturn="True"
MaxLines="2"
ScrollViewer.VerticalScrollBarVisibility="Auto"
MaxLength="150">
</TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
我的模型類從IDataErrorInfo
和INotifyPropertyChanged
翻譯是實現自定義類型「LanguageValue
」具有公共屬性鍵和值的ObservableCollection
。
我在我的模型string this[string columnName]
,它與簡單的文本框(外ItemsControl)完美的工作,但如何使這與我的項目工程?我很喜歡這樣的事情:
public string this[string columnName]
{
get
{
string result = null;
...
if (columnName == "Translations" || columnName == "ItemText")
{
if (Translations.All(t => string.IsNullOrEmpty(t.Value)))
result = Properties.Resources.errMsgEnterName;
}
...
但當然這並不奏效。
有什麼建議嗎?
很好的解決!但是當只有一個值爲空時,我不需要顯示錯誤。當他們都是空的時候我想要出錯。 – Ksice