0

As the title says, I want to hide or display some content dynamically. I have two buttons working like radio buttons. Each button has to display a specific content, but the two contents must not be visible at the same time (it's a kind of form, and I'm working on two subclasses).

I've seen on SO two interesting things, and I wanted to mix them. Firstly, the use of ContentControl with a ToggleButton (http://stackoverflow.com/questions/7698560/how-to-bind-click-of-a-button-to-change-the-content-of-a-panel-grid-using-xaml). Secondly, using ToggleButtons as RadioButtons (http://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf second answer with 31)

So I decided to start with something like that :

<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>

But Visual Studio underlines

{StaticResource {x:Type ToggleButton}}

Visually, my buttons appear like radio buttons instead of toggle buttons. Visual says :

Impossible to resolve the resource "{StaticResource {x:Type ToggleButton}}" (translated from french ;) )

Anyway this code works fine outside the ContentControl.

Sheamus
  • 223
  • 1
  • 5
  • 15

3 Answers3

1

Yeah, first of all, fix your eventHandlers. Dont attach them on templates, tricky things can happen.

Second of all, you can modify your ContentControl.Resources, and put this to there:

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

And remove your own style definitions. You can even put that one level more up(parent of ContentControl). One more thing, you're doing this in code-behind, but I can assure you, true power of WPF comes from the fact that you dont need code-behind, atleast for logic like this.

Also the link you gave, it is using triggers. This is no biggie right now but you should know that it's not correct way. If you wanna switch between contents, check out ContentControl Content, DataTemplates, DataTemplateSelector. The idea is to have only one VIEW at memory, not hide things.

That can catch up, if youre doing the other way. It will eventually add more time to startup and memory usage.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
0

In control templates you normally do not add event handlers.

Override the OnApplyTemplate and add the handler there.

Emond
  • 50,210
  • 11
  • 84
  • 115
0

I finally found another way

<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>

With code behind changing type of Trigger

Sheamus
  • 223
  • 1
  • 5
  • 15