【问题标题】:Optional Visibiltiy for Button in ControlTemplateControlTemplate 中按钮的可选可见性
【发布时间】:2013-09-22 15:15:31
【问题描述】:

对于包含关闭按钮的Tabitem,我有以下样式。

<Style x:Key="StudioTabItem" TargetType="{x:Type TabItem}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                ...
                <Button Grid.Column="2" 
                        Width="15" 
                        Height="15" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center" 
                        Visibility={Binding}> 
                ...

当我使用实际控件时,我想让StudioTabItems 按钮的可见性成为可选的。所以像

<TabControl x:Name="tabControl" 
            Style="{StaticResource StudioTabControl}"
            ItemsSource="{Binding Workspaces}" 
            SelectedIndex="{Binding SelectedIndex}"
            TabStripPlacement="Top" >
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem" 
                   BasedOn="{StaticResource StudioTabItem}"
                   IsCloseButtonVisible="False"> <-- How to do this?

见上面最后一行的IsCloseButtonVisible。我知道这很可能涉及DependencyProperties。这可能吗?我该如何实现?

感谢您的宝贵时间。

【问题讨论】:

    标签: c# wpf controls


    【解决方案1】:

    这可以通过像下面这样创建Attached Property 并在样式设置器中设置其属性来实现

        public static class TabItemBehaviour
        {
            public static readonly DependencyProperty IsCloseButtonVisibleProperty =
                DependencyProperty.RegisterAttached("IsCloseButtonVisible", typeof(bool), typeof(TabItemBehaviour), new UIPropertyMetadata(true, IsButtonVisiblePropertyChanged));
    
            public static bool GetIsCloseButtonVisible(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsCloseButtonVisibleProperty);
            }
    
            public static void SetIsCloseButtonVisible(DependencyObject obj, bool value)
            {
                obj.SetValue(IsCloseButtonVisibleProperty, value);
            }
    
            public static void IsButtonVisiblePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                TabItem item = o as TabItem;
                if (item != null)
                {
                   Button closeButton = item.Template.FindName("CloseButton", item) as Button;
                   if ((bool)e.NewValue == true)
                   {
                       closeButton.Visibility = Visibility.Visible;
                   }
                   else
                   {
                       closeButton.Visibility = Visibility.Collapsed;
                   }
                }
            }
        }
    

    然后TabItem样式设置属性即可:

    <Style TargetType="TabItem" 
                       BasedOn="{StaticResource StudioTabItem}"
                       > 
    <Setter Property="behaviours:TabItemBehaviour.IsCloseButtonVisible" Value="False"/>
    

    您还必须在您的ControlTemplate 中为Button 提供一个名称“CloseButton”

    【讨论】:

    • 非常感谢您的回复。快速提问,为什么依赖属性没有属性/访问器?
    • 附加属性有 get/set 方法.... 同样对于依赖属性,我们不需要定义属性包装器,因为 WPF 框架在访问依赖属性进行绑定时调用基类 GetValue 和 SetValue
    • 我不太明白为什么,当我输入 propdp 时,我得到的 DependencyProperty 模板与此处使用的模板不同。我对您为什么使用DependencyProperty.RegisterAttached 而不是DependencyProperty.Register 感到困惑。对不起,我很痛苦!
    • 完全没问题..这是附加属性和依赖属性之间的区别...依赖属性需要在定义它的对象上设置..例如Tabitem的背景是一个依赖属性。 . 虽然附加属性可以在任何依赖对象上设置,即使它没有定义它.. 例如,我们可以在任何控件上设置 Grid.Row,即使它没有定义它.. 在这里我们需要附加属性,这就是我使用的原因注册附件
    • 谢谢,最后一件事。我们是否能够使用像&lt;TabItem Behaviors:StudioTabItemBehaviours.IsCloseButtonVisible="False" /&gt; 这样的属性,因为这似乎是不可能的?再次,抱歉所有问题...
    猜你喜欢
    • 2018-01-21
    • 2016-11-28
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    相关资源
    最近更新 更多