對HandyControl中的Poptip進行資料繫結
阿新 • • 發佈:2021-06-25
WPF控制元件庫HandyControl中的Poptip是一個抽象物件,它不會出現在視覺化樹中,因此不能使用RelativeSource繫結。也不能使用ElementName,因為它找不到可控制的FrameworkContentElement。
通過使用Source和x:Reference可以實現這種繫結。x:Reference標記可以引用在xaml檔案中其他位置宣告的物件,這個物件需要通過x:Name命名來被引用。
必須將Poptip例項放在Resources中,例如Window.Resources,否則會因迴圈依賴而引發錯誤。
參考資料:How to hide wpf datagrid columns depending on a property
1、Resources定義示例
<Window.Resources> <hc:Poptip x:Key="MyPoptip" PlacementType="BottomLeft" Content="{Binding DataContext.SampleText, Source={x:Reference MyWindow}}"/> </Window.Resources>
2、Poptip引用示例
<TextBox Grid.Column="0" x:Name="TextBox1" Text="{Binding SampleText}" FontSize="14" Margin="2,4,4,4" IsReadOnly="True"> <hc:Poptip.Instance> <StaticResource ResourceKey="MyPoptip"/> </hc:Poptip.Instance> </TextBox>