在ItemsControl中向RadioButton添加行为(Windows 8搜索)

时间:2013-06-04 19:58:49

标签: xaml binding windows-8 itemscontrol

我正在实施Windows 8应用程序的搜索合同(结果页面)。我设计了一切跟随MVVM的东西。有了这个,我正在尝试将命令连接到Windows 8的搜索合同模板中提供的'filtersItemsControl'。

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

上面的代码是我尝试挂钩'FilterChangedCommand',它是ViewModel的一个属性。 ViewModel还包含ItemsControl绑定的属性列表(filtersViewSource)。我认为我的问题是我正在尝试将RadioButton的Command绑定到filtersViewSource(显然不存在)与ViewModel的属性。

因此,我认为这里的问题基本上是,我可以在上面的RadioButton上使用什么绑定表达式,以便它将引用ViewModel属性。

1 个答案:

答案 0 :(得分:0)

FiltersViewSource应该是某种类型的viewmodel中的属性,比如说“Foo”。在这种情况下,单选按钮将获得类型为Foo的datacontext。因此属性“FilterChangedCommand”应该在Foo类中。