简体   繁体   English

设置UserControl可见性

[英]Setting UserControl Visibility

I have a property that is not binding and cannot figure out where the problem is. 我有一个没有绑定的属性,无法确定问题出在哪里。 Can someone please help? 有人可以帮忙吗? My Converter is running for other bound controls, so there is no issue there. 我的转换器正在为其他绑定控件运行,因此那里没有问题。 When setting a break point, I never see the property IsMyControlVisible get hit. 设置断点时,我永远不会看到属性IsMyControlVisible被命中。

I added this line to the property MyControlViewModels: 我将此行添加到属性MyControlViewModels中:

this.NotifyPropertyChanged(m => m.IsMyControlVisible); 

When it get hit, it causes the set of my property to be hit, but nothing happens with the visibility of my control, even though I can see the value is true. 当它被命中时,它将导致我的属性集被命中,但是我的控件的可见性没有任何反应,即使我可以看到该值是真实的。 The control is completely functional, I just cannot get it to show programmatically. 该控件完全功能正常,我只是无法使其以编程方式显示。 Also, my visibility converter works for other views, so that is not the problem. 另外,我的可见性转换器可用于其他视图,所以这不是问题。

It seems (to me) that the binding is just not set up correctly and if I can get it working, the visibility would work on the control. (对我而言)似乎绑定设置不正确,如果我可以使它正常工作,则可见性将在控件上起作用。

My control is inside a TabControl, which is inside a Grid on my view. 我的控件在TabControl内部,而TabControl在我的视图中的Grid内部。

I've found this post that was the closest to my problem and tried it without success: 我发现这篇帖子与我的问题最接近,并尝试了但未成功:

UserControl Visibility binding through ViewModel 通过ViewModel进行UserControl可见性绑定

ConfigurationEditorView.xaml: ConfigurationEditorView.xaml:

<ctrls:MyControl Grid.Row="0" 
    Visibility="{Binding Path=IsMyControlVisible, 
    Converter={StaticResource booleanToStringConverter},
    FallbackValue=Hidden}" />

ConfigurationEditorView's Constructor: ConfigurationEditorView的构造方法:

this.DataContext = new ConfigurationEditorViewModel();

ConfigurationEditorViewModel: ConfigurationEditorViewModel:

public bool IsMyControlVisible
{
    get
    {
        return this.MyControlViewModels.Count > 0;
    }
}

private ObservableCollection<MyControlViewModel> myControlViewModels 
    = new ObservableCollection<MyControlViewModel>();

public ObservableCollection<MyControlViewModel> MyControlViewModels
{
    get
    {
        return this.myControlViewModels;
    }

    set
    {
        this.myControlViewModels = value;

        this.NotifyPropertyChanged(m => m.myControlViewModels);

        this.NotifyPropertyChanged(m => m.IsMyControlVisible);
    }
}

ConfigurationEditorViewModel Constructor: ConfigurationEditorViewModel构造函数:

public ConfigurationEditorViewModel()
{
    this.MyControlViewModels.Add(new MyControlViewModel);
}

您正在使用boolToString转换器,并且您实际上需要可见性。

As your code is written, you'll only get a notification if someone assigns a new instance of ObservableCollection to your MyControlViewModels property. 在编写代码时,只有在有人将新的ObservableCollection实例分配给MyControlViewModels属性时,您才会收到通知。 Instead, in the setter, you would write wherever you create your view models, you would add 相反,在setter中,您将在创建视图模型的任何地方编写代码,然后添加

set
{
    if (myControlViewModels != null)
    {
        myControlViewModels.CollectionChanged -= OnControlViewModelsChanged;
    }

    this.myControlViewModels = value;

    this.NotifyPropertyChanged(m => m.IsMyControlVisible);

    if (myControlViewModels != null)
    {
        myControlViewModels.CollectionChanged += OnControlViewModelsChanged;
    }
}

where 哪里

public void OnControlViewModelsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    this.NotifyPropertyChanged(m => m.IsMyControlVisible);
}

Another minor point: normally you would not have a setter for a property of type ObservableCollection - it would be get-only, and you would create it in the constructor. 另一个小问题:通常,对于类型ObservableCollection的属性,您将没有设置方法-它将是仅获取方法,并且将在构造函数中创建它。 That would simplify some of your code (like not needing to unsubscribe from the change event). 这样可以简化您的某些代码(例如不需要取消订阅change事件)。 See for instance ItemsControl.Items (it doesn't use an observable collection, the pattern is the same). 例如参见ItemsControl.Items (它不使用可观察的集合,模式相同)。

There is a BooleanToVisibilityConverter you can you use. 您可以使用BooleanToVisibilityConverter The value you are trying to get is an enum called Visibility, not a string. 您尝试获取的值是一个称为Visibility的枚举,而不是字符串。

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

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