前言
这个binding问题困扰了我几天时间了,弄了好的demo试了又试。之前老是认为是ItemsControl中数据模版DataTemplate中绑定Command问题,根据测试没问题。一直在考虑是否是绑定DataContext对象没指明同一个上下文对象问题。最后在MainWindow.xaml文件中Frame的属性Content 的Binding中加一个ElementName=Vm问题就解决了,
页面切换效果:
<FrameworkElement.DataContext>
<local1:MainWindowViewModel x:Name="Vm" />
</FrameworkElement.DataContext>
<JcFrame Name="mainFrame" AnimationMode="RightToLeft" Content="{Binding NavPage,ElementName=Vm}" />
<ItemsControl Grid.Row="1" Focusable="False" IsTabStop="False" ItemsSource="{Binding NavMenus}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Grid.Row="1" Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<!--<JcRadioButton GroupName="NavMenu" Style="{DynamicResource NavIconRadioButtonStyle}" Foreground="{Binding Path=GetThemeColor.Brush_Base1, Source={x:Static ThemeManager.Current}}" JcData="{Binding Icon}" JcDataChecked="{Binding CheckedIcon}" ToolTip="{Binding Text}" Command="{Binding NavCommand, ElementName=Vm}" CommandParameter="{Binding}" IsChecked="{Binding IsChecked}" Cursor="Hand" DisabledCursor="No" />-->
<!--Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.MouseLeftButtonDown }"/>-->
<JcRadioButton GroupName="NavMenu" Style="{DynamicResource NavIconRadioButtonStyle}" Foreground="{Binding Path=GetThemeColor.Brush_Base1, Source={x:Static ThemeManager.Current}}" JcData="{Binding Icon}" JcDataChecked="{Binding CheckedIcon}" ToolTip="{Binding Text}" Command="{Binding Path=NavCommand, ElementName=Vm}" CommandParameter="{Binding}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>