我想在DataContext = mainviewmodel中使用情节提要。 我用于情节提要的xaml代码:
<Window.Resources>
<Storyboard x:Key="showLeftMenuSB">
<ThicknessAnimation Storyboard.TargetProperty="Margin" From="-785,0,0,0" To="0,0,0,0" DecelerationRatio="0.9" Duration="0:0:1"/>
</Storyboard>
<Storyboard x:Key="hideLeftMenuSB">
<ThicknessAnimation Storyboard.TargetProperty="Margin" From="0,0,0,0" To="-785,0,0,0" DecelerationRatio="0.9" Duration="0:0:1"/>
</Storyboard>
</Window.Resources>
<StackPanel Grid.Row="1" Panel.ZIndex="1" Grid.ColumnSpan="2" Name="leftMenu" Orientation="Horizontal" Height="Auto" HorizontalAlignment="Left" Margin="0,0,0,0">
<Border Background="WhiteSmoke">
<Grid Width="785">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<view:NavigationView DataContext="{Binding NavigationViewModel}"/>
<Button Grid.Column="1" Name="leftMenuHide" Width="10" Height="80" Command="{Binding Path=HideCommand}" Content="Hide"/>
<!--"btnRightMenuHide_Click"-->
</Grid>
</Border>
<Grid>
<Button Name="lefttMenuShow" Width="10" Height="80" Click="btnRightMenuShow_Click" Content="Show"/>
</Grid>
</StackPanel>
但是我想在mainviewmodel类中绑定命令。如果我想使用此代码执行此操作
private void ShowHideMenu(string Storyboard, Button btnHide, Button btnShow, StackPanel pnl)
{
Storyboard sb = App.Current.Resources[Storyboard] as Storyboard;
sb.Begin(pnl);
if (Storyboard.Contains("Show"))
{
btnHide.Visibility = System.Windows.Visibility.Visible;
btnShow.Visibility = System.Windows.Visibility.Hidden;
}
else if (Storyboard.Contains("Hide"))
{
btnHide.Visibility = System.Windows.Visibility.Hidden;
btnShow.Visibility = System.Windows.Visibility.Visible;
}
}
然后命令
private void OnClickHide()
{
ShowHideMenu("hideLeftMenuSB",leftMenuHide, lefttMenuShow, leftMenu);
}
我有一个错误“当前环境中不存在“按钮名称”。我必须在mainviewmodel中使用它,因为如果有显示,我需要使用此命令,但是我不知道如何在MainWindow usinc MainViewModel类中使用按钮
答案 0 :(得分:0)
您需要引用视图模型中的按钮。 您可以将引用作为CommandParameter传递,因此您将引用一个按钮。 如我所见,您一次只需要一个按钮可见,因此您可以在一个按钮上使用IValueConverter,以将按钮的可见性设置为与另一个按钮的可见性相反的值。
答案 1 :(得分:0)
尝试使用EventTrigger,它会针对事件(包括动画)应用一组操作。您还可以尝试使用DataTrigger这是一个触发器,当绑定数据满足指定条件时,该触发器将应用属性值或执行操作。
然后将其用作示例:
<Style>
<Style.Trigger>
<EventTrigger RoutedEvent="{x:Static local:Yourclass.yourEvent}">
<BeginStoryboard>
.........
</BeginStoryboard>
</EventTrigger>
</Style.Trigger>
DataTrigger示例:
<Style.Triggers>
<DataTrigger
Binding="{Binding RunStoryboard}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Margin"
From="50"
To="100"
Duration="0:0:1"
RepeatBehavior="1x"
AutoReverse="False"
/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>