在CodeBehind中设置验证工具提示而不是XAML

时间:2010-05-30 14:17:20

标签: wpf validation xaml tooltip code-behind

我想知道如何将以下代码转换为代码隐藏而不是XAML:

<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip"
          Value="{Binding RelativeSource={RelativeSource Self},
          Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
</Style.Triggers>

我无法弄清楚的部分是Path部分。我有以下但它不起作用:

new Trigger
{
    Property = Validation.HasErrorProperty,
    Value = true,
    Setters = 
    {
        new Setter
        {
            Property = Control.ToolTipProperty,
            // This part doesn't seem to work
            Value = new Binding("(Validation.Errors)[0].ErrorContent"){RelativeSource = RelativeSource.Self}
         }
     }
 }

帮助?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

所以我终于想通了。事实证明我是在错误的属性上设置触发器。当我需要将它应用于ElementStyle时,我试图在DataGridTextColumn CellStyle上设置它。下面的工作代码:

new DataGridTextColumn
{
    Header = i + 1,
    Binding = new Binding(string.Format("[{0}].Value", i)) { ValidatesOnDataErrors = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged },
    ElementStyle = new Style
    {
        TargetType = typeof(TextBlock),
        Triggers =
        {        
            new Trigger
            {
                Property = Validation.HasErrorProperty,
                Value = true,
                Setters = 
                {
                    new Setter
                    {
                         Property = Control.ToolTipProperty,
                         Value = new Binding("(Validation.Errors)[0].ErrorContent"){RelativeSource = RelativeSource.Self}
                    }
                }
            }
        }
     }, ...etc