2017-01-18 4 views
0

単純な例として、ResourceControlに保存するリソースディクショナリにボタンがあります。ボタンのVisibilityプロパティをページにあるチェックボックスにバインドする必要がありますが、ボタンがの前に作成されています。の前に、私のセッターは機能しません。レイトバインディング、またはリソースディクショナリからページ要素にアクセスする

ページが初期化された後にバインドバインドを作成する方法はありますか?私はコードの背後からそれを行うことができますが、私はたくさんのボタンを持っています、そしてボタンの初期化コードの部分を別々の場所に持つのは面倒です。

<ResourceDictionary> 
<button x:Key = "MyButton">Hi There 
<button.Style> 
    <Style> 
     <Setter Property="IsVisible" Value="false"/> 
     <DataTrigger  // myCheckBox doesn't exist yet... 
      Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True"> 
      <Setter Property="IsVisible" Value="true"/> 
     <DataTrigger/> 
    </Style> 
</button.Style> 
</button> 
</ResourceDictionary> 

<Grid> 
<Grid.RowDefinitions> 
    <RowDefinition Height="1*"/> 
    <RowDefinition Height="1*"/> 
    <RowDefinition Height="1*"/> 
</Grid.RowDefinitions> 

    <CheckBox x:Name = "myCheckBox" Row=1/> //This is made too late to bind my button to 

    <ContentControl Content = "{StaticResource MyButton}" Row=2/> 

</Grid> 

私はあなたがそれらを必要とするときにオブジェクトをロードlazy loading、のようなものを見つけた、と私はmaking my own binding classを調査してきましたが、私はちょうどそれをどこに行くか分かりません。

私の現在のお気に入りのアイデアは、何かのようである:

XAML:

property="{lateBinding source=whatever path=you.want}" 

といくつかの一般的なC#クラスコード:

class lateBinding : Binding 
{ 
    OnPageInitialized() 
    { 
     SetBinding(myObject, myProperty, myBinding); 
    } 
} 

任意のアイデア?

+0

で動作します(あなたのコードを修正する必要があった問題をコンパイルしていたという事実以外の)私はあなたのコードを試してみましたが、それが正常に動作します。あなたがそれを実行しようとするとどうなりますか? –

答えて

0

あなたのコードを少し変更

<Window x:Class="WpfApplication11.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <Button x:Key = "MyButton">Hi There 
       <Button.Style> 
        <Style TargetType="{x:Type Button}"> 
         <Style.Triggers> 
          <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="True"> 
           <Setter Property="Visibility" Value="Visible"/> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding ElementName=myCheckBox, Path=IsChecked}" Value="False"> 
           <Setter Property="Visibility" Value="Collapsed"/> 
          </DataTrigger> 
         </Style.Triggers> 
        </Style> 
       </Button.Style> 
      </Button> 
     </ResourceDictionary> 
    </Window.Resources> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
     </Grid.RowDefinitions> 

     <CheckBox x:Name = "myCheckBox" Grid.Row="1"/> 

     <ContentControl Content = "{StaticResource MyButton}" Grid.Row="2"/> 

    </Grid> 
</Window> 
+0

私は自分の問題を少しでも簡略化したことが判明したので、別の質問[ここ](http://stackoverflow.com/questions/41731592/binding-elementname-failing)に尋ねました。しかし、これは間違いなく私が尋ねた質問に対する素晴らしい答えです。それは私が正しい方向に進むのを助けました。ありがとう! – bwall

関連する問題