简体   繁体   English

如何在应用程序设置中为用户控件设置样式?

[英]How to set a style for usercontrol in application setting?

[My main idea of this is to set visible/hidden for a usercontrol. [我的主要想法是为用户控件设置可见/隐藏。 I used WPF with Mvvmcross.] 我将WPF与Mvvmcross一起使用。]

I have a user control call SpinningWheelUserControl. 我有一个用户控件调用SpinningWheelUserControl。 I want to visible/hide it with the datatrigger. 我想用datatrigger看到/隐藏它。 Below is my xaml code in App.xaml 以下是我在App.xaml中的xaml代码

In App.xaml I have added the namespace of the usercontrol as below. 在App.xaml中,我添加了usercontrol的命名空间,如下所示。

xmlns:local="clr-namespace:UserControl"

The following is a style setting for my usercontrol. 以下是我的用户控件的样式设置。

<Style x:Key="SpinningWheel" TargetType="{x:Type local:SpinningWheelUserControl}" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsVisible}" Value="true">
            <Setter Property="Visibility" Value="Visible" />
        </DataTrigger>
        <DataTrigger Binding="{Binding IsVisible}" Value="false">
            <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
    </Style.Triggers>
</Style>

There is a class for SpinningWheel SpinningWheel有一个类

public class SpinningWheelViewModel 
    : MvxNotifyPropertyChanged
{
    public bool IsVisible { get; set; }
}

In a constructor of parent class, i use like this code 在父类的构造函数中,我使用像这样的代码

SpinningWheel = new SpinningWheelViewModel();
SpinningWheel.IsVisible = false;

The usercontrol is hidden for a first running. 用户控件在第一次运行时被隐藏。 But when I change the IsVisble to true, it has no change. 但是当我将IsVisble更改为true时,它没有任何变化。

SpinningWheel.IsVisible = true

You need to set Visibility instead of IsVisible like this: 您需要这样设置Visibility而不是IsVisible

SpinningWheel.Visibility = Visibility.Visible;

Oh now i see, you are setting your custom IsVisibility instead of UIElement property. 哦,现在我明白了,您正在设置自定义IsVisibility而不是UIElement属性。

Issue with your code is you haven't raised PropertyChanged to let UI know that some property change in underlying source object. 代码的问题是您没有引发PropertyChanged来让UI知道基础源对象中的某些属性发生了变化。

private bool isVisible;
public bool IsVisible
{
   get { return isVisible;}
   set
   {
      if(isVisible != value)
      {
         isVisible = value;
         RaisePropertyChanged("IsVisible");
      }
   }
}

Assuming you have implemented INotifyPropertyChanged on your class. 假设您已经在类上实现了INotifyPropertyChanged

这个n + 1个视频,称为N = 34:一个数据绑定的繁忙对话框完全显示了如何执行您要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM