【问题标题】:How to change the value of a property on toggle button IsChecked?如何更改切换按钮 IsChecked 上的属性值?
【发布时间】:2018-04-12 12:37:02
【问题描述】:

在我的基类中,我有以下内容:

    private string groupOperator;
    public string GroupOperator
    {
        get
        {
            this.groupOperator = ConvertOperatorToString();
            return this.groupOperator;
        }
        set
        {
            this.groupOperator = value;
            OnPropertyChanged("GroupOperator");
        }
    }

    private bool isOrOperator = false;
    public bool IsOrOperator
    {
        get
        {
            return this.isOrOperator;
        }
        set
        {
            this.isOrOperator = value;
            OnPropertyChanged("IsOrOperator");
        }
    }

    public string ConvertOperatorToString()
    {
        if (IsOrOperator)
        {
            return "Or";
        } 

        else
        {
            return "And";
        }

    }

我正在使用 TextBlock 在我的 XAML 中显示 GroupOperator。理想的功能是根据 Toggle Button 是否被切换,在 And / Or 之间更改字符串值。现在,它根本没有改变 TextBlock,我想知道我做错了什么。

这是我的 XAML:

<TextBlock Style="{StaticResource TextBlockBaseStyling}" VerticalAlignment="Center" Text="{Binding GroupOperator, UpdateSourceTrigger=PropertyChanged}"/>
                                            <ToggleButton x:Name="OperatorSwitch" Style="{StaticResource ToggleViewSwitch}"
                                            Visibility="{Binding IsToggleVisible, UpdateSourceTrigger=PropertyChanged}"
                                            IsChecked="{Binding IsOrOperator, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                        </Grid>
                                    </Grid>
                                    <ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
                                </Grid>
                            </Border>
                        </Grid>
                        <TextBlock Style="{StaticResource TextBlockBaseStyling}"

                                   Text="{Binding ParentGroup.GroupOperator, UpdateSourceTrigger=PropertyChanged}"
                                   Visibility="{Binding IsOperatorVisible, Converter={StaticResource BooleanConverter}, UpdateSourceTrigger=PropertyChanged}"
                                   VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0, 10, 0, 0"/>
                    </StackPanel>

注意:正确处理 PropertyChange 时,ParentGroup.GroupOperator 和 GroupOperator 实际上应该是相同的值。

【问题讨论】:

  • 您的 IsOrOperator 属性设置器应在切换 ToggleButton 时调用。在那里做任何必要的事情......
  • 什么意思?当我切换按钮并在 IsOrOperator 中设置断点时,它会被命中。它不会更改视图中 TextBlock 的文本。
  • 当然,只需更新那里的其他属性:GroupOperator = value ? "Whatever" : "Something";
  • 哇。做到了。对不起。谢谢您的帮助。我很笨,哈哈。
  • 请注意,ConvertOperatorToString() 可以这样写:return IsOrOperator ? "Or" : "And";

标签: c# wpf xaml mvvm inotifypropertychanged


【解决方案1】:

你的代码应该是

public string GroupOperator
{
    get
    {
        return ConvertOperatorToString();
    }
}

private bool isOrOperator = false;
public bool IsOrOperator
{
    get
    {
        return this.isOrOperator;
    }
    set
    {
        this.isOrOperator = value;
        OnPropertyChanged("IsOrOperator");
        OnPropertyChanged("GroupOperator");
    }
}

当设置操作符值更改多个属性时,您需要在更改时通知更多属性更改

【讨论】:

    猜你喜欢
    • 2014-02-11
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多