Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何知道何时在UserControl上调用了命令?_Wpf_Silverlight - Fatal编程技术网

Wpf 如何知道何时在UserControl上调用了命令?

Wpf 如何知道何时在UserControl上调用了命令?,wpf,silverlight,Wpf,Silverlight,我需要在执行特定ICommand后对控件进行一些视觉效果。例如,我的自定义控件公开AAACommand和BBBCommand属性 <myControl AAACommand={Binding ACommand} BBBCommand={Binding BCommand} /> 因此,在ViewModel上调用命令是没有问题的,这是毫无问题的。问题是我的用户控件如何知道AAACommand何时执行ACommand,这样它就可以用它的UI做些什么。您想根据视图模型

我需要在执行特定ICommand后对控件进行一些视觉效果。例如,我的自定义控件公开AAACommand和BBBCommand属性

<myControl AAACommand={Binding ACommand}
           BBBCommand={Binding BCommand} />

因此,在ViewModel上调用命令是没有问题的,这是毫无问题的。问题是我的用户控件如何知道AAACommand何时执行ACommand,这样它就可以用它的UI做些什么。

您想根据视图模型返回的响应和执行的命令更新usercontrol吗?我问了一个性质类似的问题,我想作为字符串值从一个用户控件传递到另一个用户控件。我使用INotifyProperty更改事件完成了此操作。您可以阅读原始问题和解决方案

最新评论:

根据你的评论,似乎有两件事可能发生。如果不需要VM响应,那么视图中的元素可能会触发更新。您可以使用Binding ElementNameProperty来实现这一点。这本质上允许您根据另一个元素的动作触发/更改属性。在一个字段中键入文本将显示另一个控件中的值

如果需要根据返回(即成功或失败)调用它,那么ViewModel需要有一个类似bool的属性,该属性双向绑定到ui中元素的属性。 您可能需要创建一个转换器继承来处理绑定,但INotifyProp更改将用于封送控件之间或控件内绑定元素之间的更新

下面是一个简单的例子:

在我的xaml中,我添加了一个用户控件,我不想在UI中看到该控件,直到单击辅助用户控件中的另一个按钮。要处理此问题,请在Visibility属性上设置绑定

<ctrl:LandingPage x:Name="ucLandingPage"
                                  Grid.Row="1" 
                                  DataContext="{Binding}"  
                                  Visibility="{Binding LandingPageVisibility, Mode=OneWay, Converter={StaticResource LandingPageVisibilityConverter}}"/>
属性注意:我使用SimpleMVVM框架,它在基本对象中包含Inotify,因此我的notify prop事件看起来可能与您的有点不同

 private Visibility _SearchVisibility;
            public Visibility SearchVisibility
            {
                get { return _SearchVisibility; }
                set
                {
                    _SearchVisibility = value;
                    NotifyPropertyChanged(m => m.SearchVisibility);
                }
            }
然后是VM中更新此属性的方法

 public void GetSearchResult()
        {
            currentPage = 1;
            //Set the visibility of the search control in the center of the page
            SearchVisibility = Visibility.Visible;
            this.SearchHistory = this._DataModel.AddSearchHistoryItem(this.SearchTerm);
        }
最后是converter类,它将返回值转换为元素的正确属性值

  public class SearchVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null & System.Convert.ToString(value) == "Visible")
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

不,我不在乎ViewModel的响应是什么。我只想在UserControl上执行此操作:调用命令,然后执行与VIewModel结果无关的自定义逻辑。我的UserControl上的操作可能是,例如,展开某个对象,或将某个对象涂成红色,等等。。。必须有某种方法来拦截UI上的命令,因为其中有一种连接机制。@Goran我已经更新了注释并提供了进一步的解释示例希望此帮助我不能使用ElementName,因为没有可以绑定的UI元素。并且该命令没有返回值。我猜解决方案在于CommandManager,但我还没有找到解决方案。最后一种方法是修改RelayCommand实现,因为它来自MVVM light framework,所以源代码是可用的。@Goran因为您使用的是MVVM light,您看过messenger api了吗?这可能是最糟糕的解决方案。最好的解决方案是完全不依赖MVVM Light,但如果我找不到任何其他解决方案,我将只对RelayCommand进行子类化,包装Execute方法,这样我就可以调用base.Execute,然后引发Executed事件。
 public void GetSearchResult()
        {
            currentPage = 1;
            //Set the visibility of the search control in the center of the page
            SearchVisibility = Visibility.Visible;
            this.SearchHistory = this._DataModel.AddSearchHistoryItem(this.SearchTerm);
        }
  public class SearchVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null & System.Convert.ToString(value) == "Visible")
            {
                return Visibility.Visible;
            }
            else
            {
                return Visibility.Collapsed;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }