2012-08-29 2 views
0

제목에서 알 수 있듯이 일부 콘텐츠를 숨기거나 표시하고 싶습니다. 라디오 버튼처럼 두 개의 버튼이 있습니다. 각 버튼은 특정 내용을 표시해야하지만 두 내용이 동시에 표시되어서는 안됩니다 (이는 일종의 양식이며 두 개의 하위 클래스에서 작업하고 있습니다).wpf 콘텐츠를 동적으로 표시/표시합니다.

저는 두 가지 흥미로운 점을 보았습니다. 나는 그것을 혼합하고 싶었습니다. 첫째, ToggleButton과 ContentControl 사용 (http://stackoverflow.com/questions/7698560/how-to-bind-click-of-a-button-to-change-the-content-of-a-panel- grid-using-xaml). 두 번째로 ToggleButton을 RadioButton으로 사용 (http://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf second answer

그래서 나는 그런 일에 시작하기로 결정) 31 :

<ContentControl> 
    <ContentControl.Template> 
     <ControlTemplate> 
      <WrapPanel HorizontalAlignment="Center"> 
       <TextBlock Text="{x:Static Internationalization:Resources.MAINOPTIONSWINDOW_STATUSLINKSUPERVISOR}"/> 
       <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Alarm" GroupName="Trigger"/> 
       <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Content="Event" GroupName="Trigger"/> 

      </WrapPanel> 
     </ControlTemplate> 
    </ContentControl.Template> 
</ContentControl> 

그러나 Visual Studio를 시각적으로

{StaticResource {x:Type ToggleButton}} 

을 강조, 내 버튼 라디오 버튼 대신 토글 버튼처럼 나타납니다. 비주얼 는 말한다 :

불가능한 자원 해결하기 위해 (프랑스어 번역) "{정적 리소스 {X 유형 ToggleButton을}}"

어쨌든이 코드는 ContentControl을 외부에서 잘 작동을).

답변

0

컨트롤 템플릿에서는 일반적으로 이벤트 처리기를 추가하지 않습니다.

OnApplyTemplate을 재정 의하여 처리기를 추가하십시오.

1

그래, 우선, 귀하의 eventHandlers 수정. 템플릿에 첨부하지 마십시오. 까다로운 일이 발생할 수 있습니다.

모든 둘째, 당신은 당신의 ContentControl.Resources을 수정할 수 있습니다, 거기에 이것을 넣어 :

<Style BasedOn="{StaticResource {x:Type ToggleButton}}" 
      TargetType="RadioButton"/> 

을 그리고 자신 만의 스타일 정의를 제거합니다. 그 한 수준을 위로 올려 놓을 수도 있습니다 (ContentControl의 부모). 한 가지 더 말하자면, 코드 숨김에서이 ​​작업을 수행하고 있지만 WPF의 진정한 힘은 코드 비하인드가 필요 없다는 사실에서 비롯됩니다.

또한 링크는 트리거를 사용하고 있습니다. 이것은 지금 큰 일이 아니지만 올바른 방법이 아님을 알아야합니다. 내용간에 전환하려면 ContentControl Content, DataTemplates, DataTemplateSelector를 확인하십시오. 아이디어는 단지 하나의 VIEW를 메모리에두고 숨기지 않는 것입니다.

다른 방식으로 수행하는 경우 따라 잡을 수 있습니다. 결국 시동 및 메모리 사용에 더 많은 시간이 추가됩니다.

0

나는 마지막으로 트리거

의 유형을 변경 뒤에 코드와 함께 또 다른 방법

<RadioButton Content="Alarm" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}" 
     Command="{x:Static StateInfoTemplateToAlarmCommand}"/> 
<RadioButton Content="Event" GroupName="TriggerStateInfo" Style="{StaticResource {x:Type ToggleButton}}" 
       Command="{x:Static StateInfoTemplateToEventCommand}"/> 
<ContentControl Content="{Binding Path=Trigger, ElementName=Window}"> 
    <ContentControl.Resources> 
     <DataTemplate DataType="{x:Type States:AlarmTrigger}"> 
      <ComboBox ItemsSource="{Binding Path=AlarmTriggersCollection}" /> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type States:EventTrigger}"> 
      <ComboBox ItemsSource="{Binding Path=EventTriggersCollection}" /> 
     </DataTemplate> 
    </ContentControl.Resources> 
</ContentControl> 

발견

관련 문제