【问题标题】:How to activate command in dependence of changed property如何根据更改的属性激活命令
【发布时间】:2020-06-30 12:39:03
【问题描述】:

我的 WPF-(弹出)控件中有两个按钮。
首先是一个取消按钮,它只是关闭弹出窗口并始终处于启用状态。

第二个是确定按钮,只有在三个(绑定)文本框之一发生更改时才应该启用它。

问题是,在文本框失去焦点之前,绑定属性的值没有改变。 因此,该按钮直到那时才处于活动状态,因此无法使用鼠标点击。

    <TextBox x:Name="tbFunction" Text="{Binding FunctionSign}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>
    <TextBox x:Name="tbSpecs" Text="{Binding Specs}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>
    <TextBox x:Name="tbSupplement" Text="{Binding Supplement}" Grid.Column="1" Grid.Row="1" VerticalContentAlignment="Center" Margin="2" Padding="10 0 0 0"/>

    <!-- Footer -->
    <Border Grid.Row="3" Padding="{Binding InnerContentPadding}">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Style="{StaticResource OkCancelButton}" Command="{Binding CloseCommand}" Content="Abbrechen"/>
            <Button Style="{StaticResource OkCancelButton}" Command="{Binding AddressChangedCommand}" Content="Ok"/>
        </StackPanel>
    </Border>

负责的视图模型代码:

    //Private Members
    private bool mPropertiesChanged = false;
    private string mFunctionSign;
    private string mSpecs;
    private string mSupplement;


    //the canExecute-method
    private bool SelectionChanged(object paramer)
    {
        return mPropertiesChanged;
    }

    //Public Properties
    public string FunctionSign
    {
        get
        {
            return mFunctionSign;
        }
        set
        {
            mFunctionSign = value;
            mPropertiesChanged = true;
            OnPropertyChanged(nameof(FunctionSign));
        }
    }
    public string Specs
    {
        get
        {
            return mSpecs;
        }
        set
        {
            mSpecs = value;
            mPropertiesChanged = true;
            OnPropertyChanged(nameof(Specs));
        }
    }
    public string Supplement
    {
        get
        {
            return mSupplement;
        }
        set
        {
            mSupplement = value;
            mPropertiesChanged = true;
            OnPropertyChanged(nameof(Supplement));
        }
    }

由于其中一个属性被更改并且相应的文本框失去焦点,一切正常。

有没有办法让这个 mPropertiesChanged 为 true,而 textbos 仍然有焦点?

【问题讨论】:

    标签: wpf binding command canexecute


    【解决方案1】:

    将绑定的UpdateSourceTrigger 设置为PropertyChanged

    Text="{Binding FunctionSign, UpdateSourceTrigger=PropertyChanged}"
    

    这将导致为每个击键设置源属性。默认值为LostFocus

    【讨论】:

    • 抱歉,忘记了。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2013-05-31
    • 1970-01-01
    • 2013-09-24
    • 2016-01-07
    相关资源
    最近更新 更多