0

我想将数据网格中的按钮变为可见或折叠,这取决于本地窗口属性。

以下项目适用于 LayoutRoot

<navigation:Page xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="TBPM.PageIssues" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 x:Name="PageIssueRoot"
...

_

        <Grid DataContext="{Binding ElementName=PageIssueRoot}">
            <Button Click="btnPasteMessage_Click" Visibility="{Binding Path=IsSaving, Converter={StaticResource BoolToVis}}" >
                <Grid>
                    <Image Height="24" Source="/Next.png" HorizontalAlignment="Center" />
                </Grid>
            </Button>
        </Grid>

代码隐藏

#Region "IsSaving"

    ''' <summary>
    ''' IsSaving Dependency Property
    ''' </summary>
    Public Shared ReadOnly IsSavingProperty As DependencyProperty = _
        DependencyProperty.Register("IsSaving", GetType(Boolean), GetType(PageIssues), _
            New Windows.PropertyMetadata(False))

    ''' <summary>
    ''' Gets or sets the IsSaving property.  This dependency property 
    ''' indicates ....
    ''' </summary>
    Public Property IsSaving() As Boolean
        Get
            Return CType(GetValue(IsSavingProperty), Boolean)
        End Get
        Set(ByVal value As Boolean)
            SetValue(IsSavingProperty, value)
        End Set
    End Property

#End Region

如果将其用作 DataGrid 模板,则相同的代码将不起作用。

解决方案是什么?为什么模板找不到根窗口?

4

1 回答 1

0

试试RelativeSource绑定...

    <Grid DataContext="{Binding ElementName=PageIssueRoot}">
        <Button Click="btnPasteMessage_Click" Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Page} Path=IsSaving, Converter={StaticResource BoolToVis}}" >
            <Grid>
                <Image Height="24" Source="/Next.png" HorizontalAlignment="Center" />
            </Grid>
        </Button>
    </Grid>
于 2012-11-09T14:53:16.907 回答