
<visualstatemanager.VisualStateGroups> <VisualStateGroup x:@R_502_6889@="LoadingStateGroup"> <VisualState x:@R_502_6889@="HistoryLoading"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.Target@R_502_6889@="HistoryGrID"> <discreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.HIDden}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:@R_502_6889@="HistoryLoaded"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.Target@R_502_6889@="WorkingStackPanel"> <discreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.HIDden}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </visualstatemanager.VisualStateGroups> 我想我知道如何改变我的代码隐藏状态(类似于此):
visualstatemanager.GotoElementState(LayoutRoot,"HistoryLoaded",true);
但是,我想要做的这个方面是在我的viewmodel的I / O完成方法中,它没有引用它的相应视图.我将如何使用MVVM模式来实现?
解决方法 你可以这样做:XAML
<Window x:Class="WpfSOTest.BusyWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-@R_502_6889@space:WpfSOTest" title="BusyWindow" Height="300" WIDth="300"><Window.Resources> <local:VisibilityConverter x:Key="VisibilityConverter" /></Window.Resources><GrID> <GrID.RowDeFinitions> <RowDeFinition /> <RowDeFinition Height="auto" /> </GrID.RowDeFinitions> <border GrID.Row="0"> <GrID> <border> <Rectangle WIDth="400" Height="400" Fill="#EEE" /> </border> <border Visibility="{Binding IsBusy,Converter={StaticResource VisibilityConverter}}"> <GrID> <Rectangle WIDth="400" Height="400" Fill="#AAA" /> <TextBlock Text="Busy" HorizontalAlignment="Center" VerticalAlignment="Center" /> </GrID> </border> </GrID> </border> <border GrID.Row="1"> <button Click="ChangeVisualState">Change Visual State</button> </border></GrID> 码:
public partial class BusyWindow : Window{ viewmodel viewmodel = new viewmodel(); public BusyWindow() { InitializeComponent(); DataContext = viewmodel; } private voID ChangeVisualState(object sender,RoutedEventArgs e) { viewmodel.IsBusy = !viewmodel.IsBusy; }}public class viewmodel : INotifyPropertyChanged{ protected Boolean _isBusy; public Boolean IsBusy { get { return _isBusy; } set { _isBusy = value; RaisePropertyChanged("IsBusy"); } } public viewmodel() { IsBusy = false; } public event PropertyChangedEventHandler PropertyChanged; public voID RaisePropertyChanged(String property@R_502_6889@) { PropertyChangedEventHandler temp = PropertyChanged; if (temp != null) { temp(this,new PropertyChangedEventArgs(property@R_502_6889@)); } }}class VisibilityConverter : IValueConverter{ public object Convert(object value,Type targettype,object parameter,System.Globalization.CultureInfo culture) { switch (((Boolean)value)) { case true: return Visibility.Visible; } return Visibility.Collapsed; } public object ConvertBack(object value,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); }} ———————–更新代码———————–
XAML
<GrID> <GrID.RowDeFinitions> <RowDeFinition /> <RowDeFinition Height="auto" /> </GrID.RowDeFinitions> <border GrID.Row="0"> <GrID> <local:Myborder IsBusy="{Binding IsBusy}"> <GrID> <TextBlock Text="{Binding IsBusy}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </GrID> </local:Myborder> </GrID> </border> <border GrID.Row="1"> <button Click="ChangeVisualState">Change Visual State</button> </border></GrID> 模板
<Style targettype="{x:Type local:Myborder}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="{x:Type local:Myborder}"> <border @R_502_6889@="Rootborder"> <border.Background> <SolIDcolorBrush x:@R_502_6889@="normalBrush" color="@R_502_3318@" /> </border.Background> <visualstatemanager.VisualStateGroups> <VisualStateGroup @R_502_6889@="CommonGroups"> <VisualState @R_502_6889@="normal" /> </VisualStateGroup> <VisualStateGroup @R_502_6889@="CustomGroups"> <VisualState @R_502_6889@="Busy"> <VisualState.Storyboard> <Storyboard> <colorAnimation Storyboard.Target@R_502_6889@="normalBrush" Storyboard.TargetProperty="color" Duration="0:0:0.5" autoReverse="True" RepeatBehavior="Forever" To="#EEE" /> </Storyboard> </VisualState.Storyboard> </VisualState> </VisualStateGroup> </visualstatemanager.VisualStateGroups> <ContentPresenter /> </border> </ControlTemplate> </Setter.Value> </Setter></Style> 自定义元素
[TemplateVisualState(Group@R_502_6889@ = "CustomGroups",@R_502_6889@ = "Busy")]public class Myborder : ContentControl{ static Myborder() { DefaultStyleKeyProperty.OverrIDeMetadata(typeof(Myborder),new FrameworkPropertyMetadata(typeof(Myborder))); } public Boolean IsBusy { get { return (Boolean)GetValue(IsBusyProperty); } set { SetValue(IsBusyProperty,value); } } public static Readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy",typeof(Boolean),typeof(Myborder),new UIPropertyMetadata(IsBusyPropertyChangedCallback)); static voID IsBusyPropertyChangedCallback(DependencyObject d,DependencyPropertyChangedEventArgs e) { (d as Myborder).OnIsBusyPropertyChanged(d,e); } private voID OnIsBusyPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { if (Convert.ToBoolean(e.NewValue)) { visualstatemanager.GoToState(this,"Busy",true); } else { visualstatemanager.GoToState(this,"normal",true); } }} 总结 以上是内存溢出为你收集整理的wpf – 如何从ViewModel更改视图中的VisualState?全部内容,希望文章能够帮你解决wpf – 如何从ViewModel更改视图中的VisualState?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)