目前,我的业务对象如下所示:
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个TextBoxes,其中每个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应该是StackPanel上的OneWay,因为您不能真正修改源代码。
发布于 2014-04-03 15:35:30
我不确定单独使用IDataErrorInfo接口是否可行--我认为这主要是为了将单个属性绑定到诸如textboxes之类的控件,并向这些控件提供验证反馈。
一种选择是在BO上公开一个布尔属性(还需要实现INPC),即
public bool IsValid
{
get { return _isValid; }
set
{
if (_isValid != value)
{
_isValid = value;
OnPropertyChanged("IsValid");
}
}
}您将在this[]属性中将此属性值设置为true或false。
在xaml中,创建一个带有DataTrigger的样式,以在StackPanel周围设置一个边框,例如:-
<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>https://stackoverflow.com/questions/22842030
复制相似问题