4
我做了一個風格,使得TextBlock
看起來像一個鏈接:條件樣式在WPF
<Style x:Key="linkStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextDecorations" Value="Underline" />
</Trigger>
</Style.Triggers>
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Foreground" Value="Blue" />
<EventSetter Event="MouseLeftButtonDown" Handler="navigateLink" />
</Style>
如何應用它,只有當TextBlock.Text
開始與的http://?
您可以使用StyleSelector。看到這篇文章http://stackoverflow.com/questions/5082509/wpf-use-a-styleselector-for-a-button – Phil 2012-02-18 12:11:36
也許你必須使用事件和自定義的C#代碼來做到這一點;默認情況下,XAML不夠智能以模仿String.StartsWith或基於正則表達式應用樣式。當然,您也可以創建自定義控件,繼承TextBlock並具有像DoesTextStartWithHttp這樣的屬性,因此您可以在XAML中使用此屬性,但對於我來說,使用事件處理程序更改C#代碼中的樣式更容易。 – Vitaliy 2012-02-18 13:08:27
可以使用自定義的C#代碼(它已經有一些'navigateLink'函數),但是我希望能夠將結果樣式應用到簡單的'TextBlock' – Poma 2012-02-18 14:07:51