0

我一直在做一个自定义窗口样式,但我在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's Resize Mode 更改属性?为了解决这个问题,我在模型的构造函数中尝试了以下内容:

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

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

我试图在 window's 之前添加以下内容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'
4

0 回答 0