【问题标题】:Binding boolean DependencyProperty to a button's Visibility property in Generic.xaml将布尔 DependencyProperty 绑定到 Generic.xaml 中按钮的 Visibility 属性
【发布时间】:2011-10-03 14:32:36
【问题描述】:

我有一个名为 CarSystemWindow 的类,它源自 Window,它有一个 CanPinWindow 布尔依赖属性:

public class CarSystemWindow : Window {

    public static readonly DependencyProperty CanPinWindowProperty =
        DependencyProperty.Register( "CanPinWindow", typeof( bool ), typeof( CarSystemWindow ),
                 new FrameworkPropertyMetadata( false, FrameworkPropertyMetadataOptions.AffectsArrange ) );

    public bool CanPinWindow {
        get { return (bool) GetValue( CanPinWindowProperty ); }
        set { SetValue( CanPinWindowProperty, value ); }
    }

    . . .
}

在 Generic.xaml 中,我为 CarSystemWindow 类定义了默认样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CarSystem.CustomControls"
                    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
                    xmlns:Telerik_Windows_Controls_Chromes="clr-namespace:Telerik.Windows.Controls.Chromes;assembly=Telerik.Windows.Controls">

    <BooleanToVisibilityConverter x:Key="BoolToVisbility" />

    <Style TargetType="{x:Type local:CarSystemWindow}">
        <Setter Property="WindowState" Value="Maximized" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CarSystemWindow}">
                    <Viewbox Name="LayoutRoot"  Stretch="Uniform">
                        <StackPanel>
                            <Grid Background="#FF3C4B66" Height="50" Name="PART_Title">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                    <ColumnDefinition Width="50" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{TemplateBinding Title}" 
                                       FontSize="32" 
                                       Foreground="White" 
                                       Grid.Column="0" 
                                       HorizontalAlignment="Left" 
                                       Name="PART_TitleLabel" />
                                <Button Grid.Column="1" 
                                        Margin="5" 
                                        Name="PART_PushpinButton" 
                                        Visibility="{Binding CanPinWindow, Converter={StaticResource BoolToVisbility}, RelativeSource={RelativeSource Self}}">
                                    <Image Name="PART_PushpinImage" Source="/CarSystem;component/Resources/Unpinned.png" />
                                </Button>
                                <Button Grid.Column="2" 
                                        Margin="5" 
                                        Name="PART_MinimizeButton">
                                    <Image Name="PART_MinimizeButtonImage" Source="/CarSystem;component/Resources/Minimize.png" />
                                </Button>
                                <Button Grid.Column="3" 
                                        Margin="5" 
                                        Name="PART_CloseButton">
                                    <Image Name="PART_CloseButtonImage" Source="/CarSystem;component/Resources/Close.png" />
                                </Button>
                            </Grid>

                            <Rectangle Fill="#FFE61E0F" Height="4" Name="PART_TitleBar" />

                            <Grid Background="#FF3C4B66" Height="25" Name="PART_SubTitle">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{TemplateBinding DeviceType}" 
                                       FontSize="22" 
                                       Foreground="White" 
                                       Grid.Column="0" 
                                       HorizontalContentAlignment="Right" 
                                       Margin="5" 
                                       MinWidth="75" 
                                       Name="PART_DeviceTypeLabel" />
                                <Label Content="{TemplateBinding DeviceName}" 
                                       FontSize="22" 
                                       Foreground="White" 
                                       Grid.Column="1" 
                                       HorizontalContentAlignment="Left" 
                                       Margin="5" 
                                       MinWidth="250" 
                                       Name="PART_DeviceNameLabel" />
                                <Rectangle Fill="White" Grid.Column="2" Name="PART_SubTitleRight" />
                            </Grid>

                            <Rectangle Fill="#FF3C4B66" Height="4" Name="PART_TitleBottom" />

                            <ContentPresenter Name="PART_ClientArea" />

                        </StackPanel>
                    </Viewbox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    . . .

</ResourceDictionary>

PART_PushpinButton 按钮的 Visibiility 属性的绑定不起作用。该按钮始终可见,即使该属性默认为 false。我做错了什么?

托尼

【问题讨论】:

    标签: wpf xaml binding ivalueconverter


    【解决方案1】:

    我认为RelativeSource 应该是TemplatedParent,而不是Self。还是我遗漏了您的某些代码?

    <Button Grid.Column="1" 
        Margin="5" 
        Name="PART_PushpinButton" 
        Visibility="{Binding CanPinWindow, Converter= {StaticResource BoolToVisbility}, RelativeSource={RelativeSource TemplatedParent}}">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      相关资源
      最近更新 更多