如何禁用自定义tabitem"关闭按钮"对于某些特定标签

时间:2015-04-18 12:50:21

标签: c# wpf xaml

我有一个带有关闭按钮的tabitem的以下XAML代码。如何在tabitem上访问(" cmdTabItemCloseButton " - >关闭按钮)。实际上我想使用不是来自XAML的c#代码来禁用某个特定选项卡的此按钮。

这是我的代码:

<Style x:Key="CustomTabItem" TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <!-- The Grid helps defining the general height of TabItems. -->
                <Grid Height="18" VerticalAlignment="Bottom" MinWidth="70">
                    <Border Name="Border"
                     Background="#FF1E1E1E"  
                     BorderThickness="0,0,1,0">
                        <Border.BorderBrush>
                            <SolidColorBrush Color="#FFC7C7C7"/>
                        </Border.BorderBrush>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="17"/>
                            </Grid.ColumnDefinitions>

                            <ContentPresenter x:Name="ContentSite"
                                     VerticalAlignment="Center"
                                     HorizontalAlignment="Center"
                                     ContentSource="Header"                                             
                                     RecognizesAccessKey="True"/>                            
                            <Button x:Name="cmdTabItemCloseButton" ToolTip="Close"
                           Style="{StaticResource TabItemCloseButtonStyle}"
                           Command="{Binding Path=Content.DataContext.CloseCommand}"
                           CommandParameter="{Binding  
                               RelativeSource={RelativeSource FindAncestor, 
                               AncestorType={x:Type TabItem}}}"
                           Grid.Column="1"
                           Margin="0,0,0,0" Click="cmdTabItemCloseButton_Click"/>
                        </Grid>
                    </Border>
                </Grid>

1 个答案:

答案 0 :(得分:0)

第一种方法

您可以在模板中找到按钮,并按以下方式启用/禁用它:

var b = (Button)tab.Template.FindName("cmdTabItemCloseButton", tab);
b.IsEnabled = ...

tabTabItem的一个实例。但是,它仅在您已加载标签时才有效,即TabItem.IsLoadedtrue。如果您想确保已加载控件,则可以订阅Loaded事件。

第二种方法

这是另一种方法,根据我的说法更优雅。它使用自定义的MyTabItem类。

MyTabItem类

这是一个基本的实现。我建议您阅读article关于INotifyPropertyChanged

namespace MyNamespace
{
    public class MyTabItem : TabItem, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private bool _isButtonEnabled;

        public bool IsButtonEnabled
        {
            get { return _isButtonEnabled; }

            set
            {
                if (value != _isButtonEnabled)
                {
                    _isButtonEnabled = value;
                    NotifyPropertyChanged();
                }
            }
        }
    }
}

如何在XAML中使用MyTabItem

<Window xmlns:myNamespace="clr-namespace:MyNamespace" ... >
    ...
    <TabControl>
        <myNamespace:MyTabItem x:Name="tab1" Style="{StaticResource CustomTabItem}"></myNamespace:MyTabItem>
        <myNamespace:MyTabItem x:Name="tab2" Style="{StaticResource CustomTabItem}"></myNamespace:MyTabItem>
    </TabControl>
</Window>

如何修改CustomTabItem样式

<Button 
    x:Name="cmdTabItemCloseButton"
    IsEnabled = "{Binding IsButtonEnabled,     
    RelativeSource=
            {RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"
...
/>

如何启用/禁用按钮

tab1.IsButtonEnabled = false;