首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ErrorTemplate for StackPanel

ErrorTemplate for StackPanel
EN

Stack Overflow用户
提问于 2014-04-03 15:19:54
回答 1查看 131关注 0票数 0

目前,我的业务对象如下所示:

代码语言:javascript
复制
    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都绑定到TextBoxProperty1TextBoxProperty2TextBoxProperty3TextBoxProperty4。我真正想要的是,如果值不等于100,则在StackPanel周围显示一个红色边框,其中包含4 TextBoxes。我的XAMl看起来会像这样:

代码语言:javascript
复制
<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,因为您不能真正修改源代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-03 15:35:30

我不确定单独使用IDataErrorInfo接口是否可行--我认为这主要是为了将单个属性绑定到诸如textboxes之类的控件,并向这些控件提供验证反馈。

一种选择是在BO上公开一个布尔属性(还需要实现INPC),即

代码语言:javascript
复制
public bool IsValid
{
    get { return _isValid; }
    set
    {
        if (_isValid != value)
        {
            _isValid = value;
            OnPropertyChanged("IsValid");
        }
    }
}

您将在this[]属性中将此属性值设置为true或false。

在xaml中,创建一个带有DataTrigger的样式,以在StackPanel周围设置一个边框,例如:-

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22842030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档