2011-03-30 89 views
3

我想將代碼中的ToolTip屬性綁定到(Validation.Errors).CurrentItem。我做媒體鏈接,與喜歡的DataGrid:綁定到(Validation.Errors).CurrentItem不適用於TextBox(適用於DataGrid)

var grid = new FrameworkElementFactory(typeof(Grid)); 
grid.SetValue(Grid.ToolTipProperty, new Binding() { 
     RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1), 
     Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent") 
}); 

這樣的作品,錯誤標誌出現在刀尖(一些錯誤文本)行標題。

當我嘗試做文本框工具提示同樣沒有出現:

grid.SetValue(Grid.ToolTipProperty, new Binding() { 
    ElementName = textBox1.Name, // tried with relative source also... 
    Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent") 
}); 

問候,

+0

是網格內的文本框(我的意思是,在一個單元格內)或網格外? – Marcote 2011-03-30 14:27:46

+0

網格只是錯誤標誌的基礎。因此,工具提示是在網格上... – Vale 2011-03-30 14:33:23

回答

1

看起來你可能能夠使用Binding.Source屬性。像這樣:

grid.SetValue(Grid.ToolTipProperty, new Binding() { 
    Source = textBox1, 
    Path = new PropertyPath("(Validation.Errors).CurrentItem.ErrorContent") 
}); 
+0

好主意,我明天將在工作中檢查它:) – Vale 2011-03-30 16:17:52

+0

工作正常。謝謝!! – Vale 2011-03-31 06:03:10

2

我不知道,你需要在那裏

我從來沒有做過驗證後面之前的代碼結合,但我對文本框驗證XAML看起來像這樣的CURRENTITEM:

<Style TargetType="{x:Type TextBox}" x:Key="ValidatingTextBox"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="True"> 
      <Setter Property="ToolTip" Value="{Binding 
        Path=(Validation.Errors)[0].ErrorContent, 
        RelativeSource={x:Static RelativeSource.Self}}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

是的,這是綁定到一個文本框本身,但我想綁定到錯誤標誌(與橢圓和TextBlock裏面的網格,看起來像(!))放置在文本框 – Vale 2011-03-30 16:14:14

+0

@Vale設置模板在觸發器而不是工具提示並覆蓋它以顯示網格和橢圓 – Rachel 2011-03-30 17:09:46

+2

@Rachel:當沒有驗證錯誤時,CurrentItem允許您綁定並且不會在控制檯中獲取錯誤。見http://stackoverflow.com/questions/2260616/why-does-wpf-style-to-show-validation-errors-in-tooltip-work-for-a-textbox-but-f – BKewl 2012-10-09 17:51:41

相關問題