簡體   English   中英

WPF,TreeViewItem,如何不顯示依賴項依賴項的contextMenu?

[英]WPF, TreeViewItem, how to not show contextMenu according to dependency property?

我有一個使用以下樣式的樹視圖:

<HierarchicalDataTemplate x:Key="itemTemplate" DataType="{x:Type AttCatalog:AttachmentCatalogModel}" ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding Name}" Tag="{Binding Guid}">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="New Item"/>
                    <MenuItem Header="Move to..." />
                    <MenuItem Header="Delete" />
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
</HierarchicalDataTemplate>

<TreeView x:Name="tree" HorizontalAlignment="Left" Width="216" BorderThickness="0,0,1,0" Background="#FFFBFBFB" IsEnabled="{Binding IsEnabled}" ItemsSource="{Binding Catalogs}" ItemTemplate="{StaticResource itemTemplate}" TreeViewItem.Expanded="OnExpandItemHandler" Margin="0,0,0,241" SelectedItemChanged="tree_SelectedItemChanged">

您可以看到TextBlock Tag綁定了Guid屬性,我的問題是,當此Guid為空(00000-00000000-00000)而不顯示contextMenu時,該怎么做?

在上下文菜單中,將可見性綁定到Guid,然后使用轉換器=>

<ContextMenu Visibility="{Binding Element=Guid, 
                          Converter={StaticResource GuidToVisibilityConverter}}">
      <MenuItem Header="New Item"/>
      <MenuItem Header="Move to..." />
      <MenuItem Header="Delete" />
</ContextMenu>

在轉換器中,您可以根據需要處理Guid值,以根據值返回Visibility.Visible或Visibility.Hidden。

您可以在此處找到有關轉換器的更多信息

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM