2016-03-18 77 views
0

這裏缺少一些東西:我在數據模板(工作)中使用輸入驗證並需要向驗證提供附加數據(不起作用)。使用「硬編碼」值(請參閱下面的代碼中的註釋)起作用,但使用綁定不適用。使用DependencyProperty綁定InputValidation附加數據

數據模式適用於顯示在listView(這也適用)的元素(具有「value」,「low_lim」和「high_lim」屬性)。我想我無法把自己的頭包裹起來。

可能我只是長期看着這個問題。如果我是對的,這是DP的某種「視覺樹」問題...

任何幫助表示讚賞。

<DataTemplate DataType="{x:Type OptionA}" > 
      <TextBox Width="100" Validation.ErrorTemplate="{StaticResource validationErrorTemplate}"> 
       <TextBox.Text> 
        <Binding Path="value" UpdateSourceTrigger="PropertyChanged"> 
         <Binding.ValidationRules> 
          <val:NumberOptionValidator> 
           <val:NumberOptionValidator.validRange> 
            <val:Range low_lim="{Binding low_lim}" high_lim="{Binding high_lim}"/> 
            <!--<val:Range low_lim="1" high_lim="2"/>--> 
           </val:NumberOptionValidator.validRange> 
          </val:NumberOptionValidator> 
         </Binding.ValidationRules> 
        </Binding> 
       </TextBox.Text> 
      </TextBox> 
    </DataTemplate> 

回答

1

我建議你在OptionA上實現IDataErrorInfo,而不是使用驗證規則。

根據https://blogs.msdn.microsoft.com/wpfsdk/2007/10/02/data-validation-in-3-5/的「當使用驗證規則與IDataErrorInfo的」一節中,對於「UI或業務層驗證邏輯」:

使用驗證規則: 驗證邏輯從數據源分離,並且可以在控件之間重用。

使用IDataErrorInfo:驗證邏輯更接近源。

下面是一些讓你開始的代碼。

XAML:

<Window x:Class="WpfApplication33.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication33" 
     xmlns:val="clr-namespace:WpfApplication33" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:VM /> 
    </Window.DataContext> 
    <Window.Resources> 
     <ControlTemplate x:Key="validationErrorTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <AdornedElementPlaceholder x:Name="textBox"/> 
       <TextBlock Text="{Binding [0].ErrorContent}" Foreground="Red"/> 
      </StackPanel> 
     </ControlTemplate> 
     <DataTemplate DataType="{x:Type local:OptionA}"> 
      <TextBox Width="100" 
        Validation.ErrorTemplate="{StaticResource validationErrorTemplate}" 
        Text="{Binding value, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" /> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <ListView ItemsSource="{Binding Items}" /> 
    </Grid> 
</Window> 

CS:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows; 

namespace WpfApplication33 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class VM 
    { 
     public List<OptionA> Items { get; set; } 
     public VM() 
     { 
      Items = new List<OptionA>() { 
       new OptionA() {value=1, low_lim = 0, high_lim = 2 }, 
       new OptionA() {value=2, low_lim = 3, high_lim = 4 }, // too low 
       new OptionA() {value=3, low_lim = 2, high_lim = 5 }, 
       new OptionA() {value=4, low_lim = 6, high_lim = 9 }, // too low 
       new OptionA() {value=5, low_lim = 0, high_lim = 4 }, // too high 
      }; 
     } 
    } 

    public class OptionA : INotifyPropertyChanged, IDataErrorInfo 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     private void OPC(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } 

     private int _value, _low_lim, _high_lim; 
     public int value { get { return _value; } set { if (_value != value) { _value = value; OPC("value"); } } } 
     public int low_lim { get { return _low_lim; } set { if (_low_lim != value) { _low_lim = value; OPC("low_lim"); } } } 
     public int high_lim { get { return _high_lim; } set { if (_high_lim != value) { _high_lim = value; OPC("high_lim"); } } } 

     #region IDataErrorInfo 
     public string Error 
     { 
      get 
      { 
       return null; 
      } 
     } 

     public string this[string columnName] 
     { 
      get 
      { 
       string err = null; 
       if (columnName == "value") 
       { 
        if (value < low_lim || value > high_lim) 
         err = string.Format("Value is out of the range of {0} to {1}.", low_lim, high_lim); 
       } 
       return err; 
      } 
     } 
     #endregion 
    } 
} 

截圖:

enter image description here

+0

無瑕,工作就像一個魅力:-)非常感謝 – Resu

相關問題