【问题标题】:Hold state of buttons dont work after binding绑定后保持按钮状态不起作用
【发布时间】:2019-09-11 14:02:33
【问题描述】:

我有一个 UWP 应用程序,其中按钮保持状态正常工作,直到我绑定(任何)按钮可见性或保持状态消失的可编辑状态。只有当我在绑定某些东西后快速点击时,这些按钮才会起作用。如果我切换到不同的视图,然后又回到原来的视图,按钮就会神奇地再次起作用。有谁知道是什么原因造成的?

将 IsHoldingState="true" 添加到 xaml 中的父级和按钮。

xml 代码

<controls:RoundedButton Grid.Row="2" Style="{StaticResource SubmitButtonStyle}" Command="{Binding SubmitCommand}" VerticalAlignment="Bottom" Visibility="{Binding IsNotEmpty, Converter={StaticResource visibilityConverter}, ConverterParameter=false}" Margin="-16,-16,-32,-32" Width="384" Height="112" Opacity="0" Background="Transparent" />


<controls:RoundedButton Grid.Row="2" Style="{StaticResource SubmitButtonStyle}" Command="{Binding SubmitCommand}" VerticalAlignment="Bottom" Visibility="{Binding IsNotEmpty, Converter={StaticResource visibilityConverter}, ConverterParameter=false}">

cs 代码(通过 onpropertychanged 更新绑定)

                      private bool isNotEmpty;
    public bool IsNotEmpty
    {
        get { return isNotEmpty; }
        set { Set(() => IsNotEmpty, ref isNotEmpty, value); }
    }

        protected bool Set<T>(Expression<Func<T>> selectorExpression, ref T field, T value)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        RaisePropertyChanged(selectorExpression);
        return true;
    }


        protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> selectorExpression)
    {
        var propertyName = GetPropertyName(selectorExpression);
        OnPropertyChanged(propertyName);
    }


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

【问题讨论】:

  • 您好,我无法重现您的问题,能否提供问题代码,帮助我解决问题。另一个是当你回滚页面时,如果你不缓存页面,页面的状态会被刷新,这可能是你的按钮再次起作用的原因。
  • 您好,当然,添加了一些可能有用的信息。

标签: c# xaml mvvm uwp


【解决方案1】:

问题可能出在你的ConverterParameterConverterParameter不能直接赋值给boolean值,请试试这个:

<Page.Resources>
    <x:Boolean x:Key="DefaultParameter">False</x:Boolean>
</Page.Resources>
...
<controls:RoundedButton Visibility="{Binding IsNotEmpty, Converter={StaticResource visibilityConverter}, ConverterParameter={StaticResource DefaultParameter}}">

最好的问候。

【讨论】:

    猜你喜欢
    • 2014-09-04
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多