WPF:窗口样式中的DataContext不起作用

时间:2013-06-18 06:49:40

标签: wpf styles window datacontext

我一直在做自定义窗口样式,但我在设置样式DataContext时遇到问题...

实际上,我认为DataContext没有错,但我可能试图用错误的方式做到这一点。

我想设置按钮(例如,最大化和最小化按钮)基于窗口调整大小模式的可见性(尽管需要说我需要以某种方式在风格中执行此操作)。

以下是使用DataContext的方法:

<!-- Inside window style -->
<ControlTemplate TargetType="{x:Type Window}">
    <Grid Name="grid" >
        <Grid.DataContext>
            <local:ButtonVisibilityViewModel />
         </Grid.DataContext>

现在,在这个网格中,我放置了一个DockPanel,其中包含上面提到的按钮 - 需要相应地设置其可见性:

<DockPanel DataContext="{Binding Path=ButtonVisibility, Mode=TwoWay}" x:Name="TitleBar" Panel.ZIndex="1" LastChildFill="true" HorizontalAlignment="Stretch" Height="30" VerticalAlignment="Top" Width="Auto">
    <Button Name="btnMaximize" Visibility="{Binding Path=MaximizeVisibility}" BorderThickness="0,0,0,1" BorderBrush="Aqua" DockPanel.Dock="Right" Margin="0,-8,0,0"  Width="50" Height="18" Click="btnMaximize_Click">
        Maximize
    </Button>

我相信到目前为止一切都还可以,但我的模型可能会导致问题。 我怎么知道哪个窗口“调用”所有这些因此根据win的调整大小模式改变属性? 为了解决这个问题,我在模型的构造函数中尝试了以下内容:

Window win = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

事情是,当调用模型时,没有活动窗口,我错了吗? 也就是说,win将为null并且不会更改可见性属性。

我尝试在窗口InitializeComponent()之前添加以下内容,但它仍然没有改变任何内容:

this.DataContext = new ButtonVisibilityViewModel();

我相信我真的很接近解决方案,但是 - 就像我之前提到的那样 - 我可能会使用错误的方法。

有人能帮帮我吗? 此致

@Edit - 输出 - 将 PresentationTraceSources.TraceLevel = High 添加到每个按钮绑定为 rhe1980 建议:

System.Windows.Data Warning: 55 : Created BindingExpression (hash=1617227) for Binding (hash=12453295)
System.Windows.Data Warning: 57 :   Path: 'MaximizeVisibility'
System.Windows.Data Warning: 59 : BindingExpression (hash=1617227): Default mode resolved to OneWay
System.Windows.Data Warning: 60 : BindingExpression (hash=1617227): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 61 : BindingExpression (hash=1617227): Attach to System.Windows.Controls.Button.Visibility (hash=60223391)
System.Windows.Data Warning: 66 : BindingExpression (hash=1617227): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=1617227): Found data context element: Button (hash=60223391) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=1617227): DataContext is null
System.Windows.Data Warning: 64 : BindingExpression (hash=1617227): Resolve source deferred
System.Windows.Data Warning: 55 : Created BindingExpression (hash=53239939) for Binding (hash=40823060)
System.Windows.Data Warning: 57 :   Path: 'MaximizeVisibility'
System.Windows.Data Warning: 59 : BindingExpression (hash=53239939): Default mode resolved to OneWay
System.Windows.Data Warning: 60 : BindingExpression (hash=53239939): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 61 : BindingExpression (hash=53239939): Attach to System.Windows.Controls.Button.Visibility (hash=35353884)
System.Windows.Data Warning: 66 : BindingExpression (hash=53239939): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=53239939): Found data context element: Button (hash=35353884) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=53239939): DataContext is null
System.Windows.Data Warning: 64 : BindingExpression (hash=53239939): Resolve source deferred
System.Windows.Data Warning: 66 : BindingExpression (hash=1617227): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=1617227): Found data context element: Button (hash=60223391) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=1617227): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=53239939): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=53239939): Found data context element: Button (hash=35353884) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=53239939): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=1617227): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=1617227): Found data context element: Button (hash=60223391) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=1617227): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=53239939): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=53239939): Found data context element: Button (hash=35353884) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=53239939): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=1617227): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=1617227): Found data context element: Button (hash=60223391) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=1617227): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=53239939): Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=53239939): Found data context element: Button (hash=35353884) (OK)
System.Windows.Data Warning: 70 : BindingExpression (hash=53239939): DataContext is null
System.Windows.Data Warning: 66 : BindingExpression (hash=1617227): Resolving source  (last chance)
System.Windows.Data Warning: 69 : BindingExpression (hash=1617227): Found data context element: Button (hash=60223391) (OK)
System.Windows.Data Warning: 77 : BindingExpression (hash=1617227): Activate with root item <null>
System.Windows.Data Warning: 105 : BindingExpression (hash=1617227):   Item at level 0 is null - no accessor
System.Windows.Data Warning: 79 : BindingExpression (hash=1617227): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 87 : BindingExpression (hash=1617227): TransferValue - using fallback/default value 'Visible'
System.Windows.Data Warning: 88 : BindingExpression (hash=1617227): TransferValue - using final value 'Visible'
System.Windows.Data Warning: 66 : BindingExpression (hash=53239939): Resolving source  (last chance)
System.Windows.Data Warning: 69 : BindingExpression (hash=53239939): Found data context element: Button (hash=35353884) (OK)
System.Windows.Data Warning: 77 : BindingExpression (hash=53239939): Activate with root item <null>
System.Windows.Data Warning: 105 : BindingExpression (hash=53239939):   Item at level 0 is null - no accessor
System.Windows.Data Warning: 79 : BindingExpression (hash=53239939): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 87 : BindingExpression (hash=53239939): TransferValue - using fallback/default value 'Visible'
System.Windows.Data Warning: 88 : BindingExpression (hash=53239939): TransferValue - using final value 'Visible'

0 个答案:

没有答案