2014-04-03 24 views
0

目前,我的業務對象是這樣的:ErrorTemplate爲StackPanel中

public class FooBar : IDataErrorInfo 
    { 
     public Int32 TextBoxProperty1 { get; set; } 
     public Int32 TextBoxProperty2 { get; set; } 
     public Int32 TextBoxProperty3 { get; set; } 
     public Int32 TextBoxProperty4 { get; set; } 
     public Int32 Total{ get; set; } 

     public override Boolean Validate() 
     { 
      if (Total < 100) 
      { 
       throw new Exception(); 
      } 

      return true; 
     } 

     public string Error 
     { 
      get { throw new NotImplementedException(); } 
     } 

     public string this[String propertyName] 
     { 
      get 
      { 
       if (propertyName == "Total") 
        if (Validate()) 
         return "Error"; 

       return null; 
      } 
     } 
    } 

現在目前在我的XAML(這是一個ResourceDictionary)我DataTemplate中持有4個文本框,其中每個TextBox勢必TextBoxProperty1TextBoxProperty2TextBoxProperty3TextBoxProperty4。我真正想要的是,如果值不等於100,那麼在StackPanel周圍會顯示一個紅色邊框,其中包含4 TextBoxes。我的XAML會是這個樣子:

<DataTemplate x:Key="MyTemplate"> 
     <StackPanel Style="{StaticResource errorSPStyle}"> 
      <StackPanel.DataContext> 
       <Binding Path="Parameter" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged" /> 
      </StackPanel.DataContext> 
      <pres:TextBox Width="40"> 
        <pres:TextBox.Text> 
         <Binding Path="Parameter.TextBoxProperty1" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True"> 
         </Binding> 
        </pres:TextBox.Text> 
       </pres:TextBox> 
      <pres:TextBox Width="40"> 
        <pres:TextBox.Text> 
         <Binding Path="Parameter.TextBoxProperty2" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True"> 
         </Binding> 
        </pres:TextBox.Text> 
       </pres:TextBox> 
      <pres:TextBox Width="40"> 
        <pres:TextBox.Text> 
         <Binding Path="Parameter.TextBoxProperty3" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True"> 
         </Binding> 
        </pres:TextBox.Text> 
       </pres:TextBox> 
      <pres:TextBox Width="40"> 
        <pres:TextBox.Text> 
         <Binding Path="Parameter.TextBoxProperty4" Mode="TwoWay" ValidatesOnExceptions="True" NotifyOnValidationError="True"> 
         </Binding> 
        </pres:TextBox.Text> 
       </pres:TextBox> 

什麼辦法可以讓風格在我StackPanel而不是在TextBox應用?我知道Binding應該是OneWay,StackPanel因爲你不能真正修改源代碼。

+0

請刪除所有立即使用'ItemsControl'。 –

回答

0

我不知道這是可能單獨使用IDataErrorInfo的接口 - 我認爲這主要是針對個人的結合性質的控制,如文本框,並以這些控件提供的驗證反饋。

一種選擇是暴露在你的BO布爾屬性(也需要實現INPC),即

public bool IsValid 
{ 
    get { return _isValid; } 
    set 
    { 
     if (_isValid != value) 
     { 
      _isValid = value; 
      OnPropertyChanged("IsValid"); 
     } 
    } 
} 

你會設置該屬性值設置爲true或false從this[]屬性中。

在XAML中,創建一個樣式與DataTrigger樣式周圍的StackPanel邊框,e.g.:-

<Style x:Key="BorderStyle" TargetType="Border"> 
    <Setter Property="BorderBrush" Value="White"/> 
    <Setter Property="BorderThickness" Value="1"/> 
    <Style.Triggers> 
    <DataTrigger Binding="{Binding IsValid}" Value="False"> 
     <Setter Property="BorderBrush" Value="Red"/> 
    </DataTrigger> 
    </Style.Triggers> 
</Style> 

<Border Style="{StaticResource BorderStyle}"> 
    <StackPanel ..etc.. 
</Border> 
+0

要給這個鏡頭 –

+0

這絕對有效,但我想知道是否仍然可以在一組控件周圍放置紅色邊框。它不一定是一個StackPanel。有另一種方法嗎? –