目前,我的業務對象是這樣的: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
勢必TextBoxProperty1
,TextBoxProperty2
,TextBoxProperty3
,TextBoxProperty4
。我真正想要的是,如果值不等於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
因爲你不能真正修改源代碼。
請刪除所有立即使用'ItemsControl'。 –