BooleanToVisibilityConverter无法正常工作

时间:2014-11-30 22:39:15

标签: c# wpf xaml mvvm

我有以下MainWindow.xaml文件,我将ConnectionStatus.cs文件定义为DataContext。我想隐藏" AccView" UserControl如果没有连接到外部设备:

<Window x:Class="Overvaagning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vw="clr-namespace:Overvaagning.View"
    xmlns:vm="clr-namespace:Overvaagning.ViewModel"
    Title="MainWindow" Height="619.631" Width="790.181">
<Window.Resources>
    <ResourceDictionary>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View\resStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Window.DataContext>
    <vm:ConnectionStatus/>
</Window.DataContext>

<Grid Background="{StaticResource brushMainWindow}">
    <vw:AccView x:Name="AccView" Margin="0,10,96,141" Visibility="{Binding Path=UserControlVisible, Converter={StaticResource BoolToVisConverter}}" />
</Grid>

和我的ConnectionStatus.cs类如下:

public class ConnectionStatus : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public connModel ConStatus { get; set; }

    public ConnectionStatus()
    {
        ConStatus = new connModel();
        ConStatus.PropertyChanged += con_PropertyChanged;
        ConStatus.countDevices(); 
    }



    private bool _userControlVisible = false;
    public bool UserControlVisible
    {
        get { return _userControlVisible; }
        set
        {
            if (value != _userControlVisible)
            {
                _userControlVisible = value;
                OnPropertyChanged("UserControlVisible");
            }
        }
    }

    protected virtual void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private void con_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "statusVisible":
                UserControlVisible = ConStatus.statusVisible;
                break;
        }

    }
}

在启动期间,bool UserControlVisible实际上从false更改为true,即使触发了OnpropertyChanged方法,MainWindow.xaml文件也不会显示UserControl。它仍然是隐藏的&#34;。问题出在哪里?

connModel.cs类:

public class connModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public connModel()
    {

    }

    public bool statusVisible { get; set; }

    public async void countDevices()
    {
        var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.GenericAccess));
        if (devices.Count == 0)
        {
            statusVisible = false;

        }
        else 
        {
            statusVisible = true;            
        }
        OnPropertyChanged("statusVisible");
        OnPropertyChanged("Status");
    }

    protected virtual void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

 }

2 个答案:

答案 0 :(得分:1)

由于某些原因,绑定到其他类的成员不起作用。这对我有用:

在窗口的C#代码中,创建一个指向ConnectionStatus的公共属性,例如:

public class MainWindow : Window
{
    // ...

    public ConnectionStatus CurrentConnectionStatus
    {
        get { return _myConnectionStatus; }
        // set is optional
    }
}

不得自动实施该属性。

不是设置窗口的数据上下文,而是将x:Name属性设置为某个值,例如“MainWindowObj”,将绑定的ElementName设置为此值,并将属性的名称添加到路径中,例如:

<Window ...
        x:Name="MainWindowObj">
    ...
        <vw:AccView ... Visibility="{Binding ElementName=MainWindowObj, Path=CurrentConnectionStatus.UserControlVisible, Converter={StaticResource BoolToVisConverter}}"/>

答案 1 :(得分:0)

我认为如果您可以发布您的VM代码会有所帮助。这是一些想法。

我总是在同一个View / UserControl / etc中使用ResourceDictionary和其他东西时遇到了麻烦。传统上,我会将所有转换器放在我的词典中。也许,你也应该这样做。例如,从这里拿出来

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="View\resStyles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

并将其放入您的ResourceDictionary中。你应该能够像这样直接复制和粘贴它

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />

   ...

</ResourceDictionary>

此外,布尔值默认为false:http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx。希望这会有所帮助。