カスタム ウィンドウ スタイルを作成しましたが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 - 出力 - rhe1980が提案したように、 PresentationTraceSources.TraceLevel=Highを各ボタン バインディングに追加しました。
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'