开发者

Set Validation Tooltip in CodeBehind instead of XAML

I would like to know how to translate the following code to codebehind instead of 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>

The part I can't figure out is the Path portion. I have the following but it doesn't work:

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

Help?


How to Create a Binding in Code (MSDN).


So I finally figured this out. It turns out that I was setting the trigger on the wrong property. I was trying to set it on the DataGridTextColumn CellStyle when I needed to apply it to the ElementStyle. Working code below:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜