绑定未按预期更新

时间:2017-07-09 18:48:46

标签: c# wpf data-binding

我有一个预加载器屏幕,基本上说“请等待”,因为我的服务器端计算处理了几秒钟。

我有一个should值转换器,一旦处理并存储了服务器端计算,就会更新并删除加载器屏幕。

WPF部分

<Window.Resources>
    <Client:BoolToVisibilityConverter x:Key="loadConverter"/>
</Window.Resources>
.
.
.
    <Border Panel.ZIndex="1000" BorderBrush="Yellow" BorderThickness="1" Visibility="{Binding OverlayVisibility, Converter={StaticResource loadConverter}, Mode=TwoWay}" Background="#80000000" Margin="0,0,0,-25.6">
        <Grid>
            <TextBlock Panel.ZIndex="100" Margin="0" TextWrapping="Wrap" Text="Loading Passive Seismic Nodes..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="21" FontWeight="Bold" Foreground="#FFF"/>
            <TextBlock Panel.ZIndex="100" Margin="11,136,12,75.2" TextWrapping="Wrap" Text="Please Wait..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" Foreground="#FFF"/>
        </Grid>
    </Border

我在这个类中有一个OverlayVisibility属性,它是一个布尔值,可以帮助切换预加载器屏幕。

客户端类的部分

    public void LoadRoles()
    {
        foreach (var roleName in ChefServer.GetCookbookNames())
        {
            Cookbooks.Add(new Cookbook() { CookbookName = roleName });
        }
        //This isn't making the preloader disappear
        uiContext.Send((_ => { overlayVisibility = false; }), null);
        Console.WriteLine("Done!"); //This gets called successfully
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }


    //This function gets called when WPF form loads
    public void Loader()
    {

        uiContext = SynchronizationContext.Current; //Declared at top in namespace
        OverlayVisibility = true; //Make preloader screen show at boot
    }


    #region Props
    private bool overlayVisibility;
    public bool OverlayVisibility
    {
        get { return overlayVisibility; }
        set
        {
            overlayVisibility = value;
            OnPropertyChanged("OverlayVisibility");
        }
    }
#endregion

2 个答案:

答案 0 :(得分:2)

您正在设置overlayVisibility(字段),而不是OverlayVisibility(属性)。

因此,你实际上从未提出过PropertyChanged,而WPF从未发现过。

答案 1 :(得分:0)

您确定已正确设置DataContext吗?如果您尚未设置,请尝试将以下行添加到您的c

this.DataContext = this;