0

I think I have the same problem described in this question.

<ItemsControl ItemsSource="{Binding Buttons}">
    ...
    <DataTemplate>
        <RadioButton Style="{StaticResource {x:Type ToggleButton}}"/>
    ...

In this case the static resource is not resolved. I tried changing it to a DynamicResource as per the link above but this doesn't fix the problem. What can I do to access the StaticResource in a DataTemplate ?

Community
  • 1
  • 1
Sherlock
  • 5,557
  • 6
  • 50
  • 78
  • how and where is the style that you are referencing defined? – lisp Oct 13 '13 at 12:14
  • I'm not defining it ... not so familiar with this side of XAML yet. Do I need to define it somewhere ? – Sherlock Oct 13 '13 at 12:20
  • if you want to use just the default style for ToggleButton (not your overriden default style) on your RadioButton, then no. But this works for me. How do you know that the resource is not resolved? – lisp Oct 13 '13 at 12:24
  • If the style is resolved, then I should only be able to select one RadioButton at a time. If not then I can select as many as I like. When I define the RadioButtons outside of the DataTemplate it works fine. Inside a DataTemplate it doesn't work. I'm also getting a 'could not be resolved' error in XAML designer. – Sherlock Oct 13 '13 at 12:31
  • The one-of-a-group behaviour of RadioButton is controlled by GroupName property of RadioButton and styles have nothing to do with it. Besides, when you apply TogglButton style to RadioButton, they don't look like RadioButtons anymore. – lisp Oct 13 '13 at 12:35
  • The reason I can't use the GroupName property is because this is in a user control that will be used mutliple times in a window. I need the group to apply to each user control independently. This might explain more http://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf – Sherlock Oct 13 '13 at 12:40

1 Answers1

1

If you don't define GroupName for radio buttons, then they will group themselves according to their parent container, i.e. all radios in a single stack panel/grid/dock panel will behave as if they have the same group name. Can you change your UI so that all buttons will reside in the same container?

If that is not possible, define a property of type String on your user control and then bind GroupName (on each radio button) to that property

GroupName="{Binding RelativeSource={RelativeSource FinsAncestor, AncestorType={x:Type my:MyUserControl}}, Path=CurrentGroupName}"

Then, on each instance of the user control, you can set a different group name.

XAMeLi
  • 6,189
  • 2
  • 22
  • 29
  • I put them all in separate Grid containers. Issue still occurs. I'm trying my best to avoid creating a random string for each user control as the GroupName but it looks like I may have to go down this route. – Sherlock Oct 15 '13 at 07:56
  • You don't have to create a random string, let the user of your `UserControl` (i.e. the developer that creates an instance of the UC) to define the group. As long as the property is `DependencyProperty`, they will be able to bind it to a property on their view model if they need to. – XAMeLi Oct 17 '13 at 05:03