Search code examples
xamlbindingwindows-8itemscontrol

Add behavior to RadioButton in ItemsControl (Windows 8 Search)


I'm implementing the search contract (results page) of a Windows 8 application. I've designed everything to follow MVVM. With that, I'm trying to wire up a command to the 'filtersItemsControl' that is provided in the search contract template for Windows 8.

<ItemsControl
                x:Name="filtersItemsControl"
                Canvas.ZIndex="1"
                ItemsSource="{Binding Source={StaticResource filtersViewSource}}"
                Visibility="{Binding ShowFilters, Converter={StaticResource BooleanToVisibilityConverter}}"
                Margin="120,-3,120,30">

                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <RadioButton
                            GroupName="Filters"
                            IsChecked="{Binding Active, Mode=TwoWay}"
                            common:CheckedCommandBehavior.Command="{Binding RelativeSource={RelativeSource Self}, Path=FilterChangedCommand}"
                            Style="{StaticResource TextRadioButtonStyle}">
                            <TextBlock Text="{Binding Description}"  Margin="3,-7,3,10" Style="{StaticResource GroupHeaderTextStyle}" />
                        </RadioButton>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

The code above is my attempt to hook into 'FilterChangedCommand' which is a property of the ViewModel. The ViewModel also contains a list of properties that the ItemsControl is bound to (filtersViewSource). I think my issue is that I'm trying to bind the Command of the RadioButton to a property of filtersViewSource (which obviously does not exist) vs. the ViewModel.

So with that, I THINK the question here is basically, what binding expression can I used on the above RadioButton such that it will reference the ViewModel property.


Solution

  • FiltersViewSource should a property in viewmodel of some type, say "Foo". In that case, radio button will get datacontext of type Foo. So the property "FilterChangedCommand" should be in Foo class.