繁体   English   中英

UWP ToggleButton 文本取决于 IsChecked C#

[英]UWP ToggleButton text depending on IsChecked C#

我想在 UWP 应用程序中将按钮文本从“运行”更改为“停止”并向后更改,并更改单击标志的布尔属性。 我尝试使用 Microsoft XamlBehaviors 库。

XAML:

<ToggleButton x:Name="ToggleButton" Grid.Row="1" Grid.Column="0"
                      Height="79" HorizontalAlignment="Stretch" Margin="33,0,-74,0"
                      IsChecked="{Binding IsGraphRenderingEnabled, Mode=TwoWay}"
                      Command="{Binding ToogleGraphRendering}">

            <Grid>
                <interactivity:Interaction.Behaviors>
                    <core:DataTriggerBehavior
                        Binding="{Binding IsChecked, 
                            ElementName=ToggleButton}"
                        Value="True" />
                    <core:DataTriggerBehavior
                        Binding="{Binding IsChecked, 
                            ElementName=ToggleButton}"
                        Value="False" />
                    <core:ChangePropertyAction TargetObject="{Binding GraphRenderingButtonText}"
                                               PropertyName="Content"
                                               Value="Run" />

                </interactivity:Interaction.Behaviors>

            </Grid>
        </ToggleButton>

后面的代码:

 public MainViewModel()
    {
        InitializeCommands();
    }
    private bool _isGraphRenderingEnabled;

    public bool IsGraphRenderingEnabled
    {
        get => _isGraphRenderingEnabled;
        set => SetField(ref _isGraphRenderingEnabled, value);
    }

    private string _graphRenderingButtonText;
    public string GraphRenderingButtonText
    {
        get => _graphRenderingButtonText;
        private set => SetField(ref _graphRenderingButtonText, value);
    }
    
    private void InitializeCommands()
    {
        ToogleGraphRendering = new RelayCommand(StartStopRendering);
    }

    private async void StartStopRendering()
    {
        if (IsGraphRenderingEnabled)
        {
            GraphRenderingButtonText = "Stop";
            var contentDialog = new ContentDialog
            {
                Title = "Attention!",
                Content = "Are you sure to stop rendering?",
                PrimaryButtonText = "ОК",
                SecondaryButtonText = "Cancel"
            };

            await contentDialog.ShowAsync();
        }
    }

它工作不正确。 我无法更改按钮文本。 所以我认为我在 Xaml Behaviors 中有错误,但我不知道在哪里......

我会将以下代码添加到您的MainViewModel类中:

public string RenderToggleText(bool? c)
    => c is bool check && check ? "Stop" : "Run";

并在 XAML 中定义您的ToggleButton ,如下所示:

<ToggleButton x:Name="ToggleButton" Grid.Row="1" Grid.Column="0"
              Height="79" HorizontalAlignment="Stretch" Margin="33,0,-74,0"
              IsChecked="{Binding IsGraphRenderingEnabled, Mode=TwoWay}"
              Command="{Binding ToogleGraphRendering}"
              Content="{x:Bind RenderToggleText(ToggleButton.IsChecked), Mode=OneWay}"/>

现在将在ToggleButtonIsChecked值更改时调用RenderToggleButton方法,从而相应地更新其内容。

由于您似乎已经有一个实现INotifyPropertyChangedGraphRenderingButtonText属性,您也可以将ToggleButtonContent绑定到此属性:

Content="{x:Bind GraphRenderingButtonText, Mode=TwoWay}"

然而,使用第一种方法,您不再需要此属性,因此您不必更改它,因为它是自动完成的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM