2017-01-18 21 views
0

간단한 예를 들어 리소스 사전에 버튼이있어 ContentControl에 저장합니다. 단추의 Visibility 속성을 페이지에있는 확인란에 바인딩해야하지만 단추가 앞에 표시되어 전에 확인란을 선택하면 설정자가 작동하지 않습니다.Late Binding 또는 리소스 사전에서 페이지 요소에 액세스

페이지가 초기화 된 후 바인딩 바인딩을 만드는 방법이 있습니까? 코드 뒤에서 할 수는 있지만 버튼이 많을 것이며 단추의 초기화 코드 일부를 다른 위치에 두는 것이 다소 번거 롭습니다.

<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