在WPF

2016-12-28 34 views
1

文本塊動態提示我是有一些麻煩,在文本塊WPF工具提示。在WPF

後,我完成任務1,如果是任務的錯誤,我想更新提示錯誤信息。 但是工具提示在完成時從不顯示。請幫幫我 。 感謝

這裏我的代碼 C#代碼

if (status == "Error") 
      { 
       LogCreateSite item = (LogCreateSite)gridLog.Items[rowIndex]; 
       item.ErrorInfo = "Error"; 
       DataTemplate template = cellTitle.ContentTemplate; 
       Canvas canvas = (Canvas)template.LoadContent(); 
       TextBlock txtError = (TextBlock)canvas.Children[1]; 
       ToolTip toolTip = new ToolTip(); 
       toolTip.Content = "asdfasdf"; 
       txtError.ToolTip = toolTip; 
       txtError.UpdateLayout(); 
      } 

而且我的XAML:

<DataTemplate x:Key="error"> 
      <Canvas Margin="10,15,0,0"> 
       <!--<Ellipse Fill="#FF5050" Width="12" Height="12"> 
       </Ellipse>--> 

       <Viewbox Width="16" Height="16"> 
        <Frame Source="../Icon/Error_16.xaml" /> 
       </Viewbox> 

       <TextBlock Text="Error" Margin="25,-3,0,0"> 
       </TextBlock> 
       <TextBlock Cursor="Hand" Name="txtErrorInfo" ToolTip="{Binding ErrorInfo, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" FontSize="14" Text="?" Margin="60,-3,0,0" FontWeight="Bold" Foreground="Blue"> 

       </TextBlock> 
      </Canvas> 
     </DataTemplate> 
+0

爲什麼你不能使用綁定? – Prajwal

+0

我嘗試綁定,但它不與我合作:(.ToolTip =「{綁定錯誤}」 – jonny

+0

你需要一個屬性,它有'OnPropertyChanged'。 – Prajwal

回答

2

你需要做的將刀頭結合顯示錯誤/信息給用戶。

請去通過這個教程WPF結合。來自WPF-tutorial的Introduction to WPF data binding

你的XAML應該是這樣的一個適當的結合。

Name="txtErrorInfo" ToolTip="{binding path=error mode=OneWay UpdateSourceTrigger=PropertyChanged}" 

,要求提modeUpdateSourceTrigger當你正在改變應顯示用戶的屬性。

+0

謝謝你,但是當我喜歡這個工具提示結合=「{結合ERRORINFO,模式=單向,UpdateSourceTrigger =的PropertyChanged}」。工具提示尚未顯示。你能告訴我爲什麼嗎 。我的代碼在主題中更新。可能是我在DataTemplate中有textblock。 – jonny

+0

'ErrorInfo'應該是一個字符串 – Prajwal

+0

是的。當然ErrorInfo是字符串:)。但是錯誤不能顯示:( – jonny

1

糾正你的代碼,看到這個樣本如何顯示從代碼ToolTip

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     ToolTip t = new ToolTip(); 
     t.Content = DateTime.Now.ToString(); 
     t.IsOpen = true; 
     t.PlacementTarget = txtError; 
     t.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom; 

     txtError.ToolTip = t; 
    } 
+0

謝謝工具提示顯示工具提示,但車展當任務完成情況:(。我要提示顯示當用戶鼠標懸停錯誤:( – jonny

+0

應用相應的邏輯。 – AnjumSKhan

+0

現在我有一個任務,這個任務如果完成我告訴完成的圖像,如果任務錯誤-i顯示了錯誤的圖像和TextBlock的「?」,當用戶的鼠標懸停這個文本塊我用工具提示顯示消息錯誤:)。謝謝 – jonny