【问题标题】:UWP ToggleButton text depending on IsChecked C#UWP ToggleButton 文本取决于 IsChecked C#
【发布时间】:2021-01-26 21:48:44
【问题描述】:

我想在 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 行为中有错误,但我不知道在哪里......

【问题讨论】:

    标签: c# .net uwp uwp-xaml


    【解决方案1】:

    我会将以下代码添加到您的 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}"
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      相关资源
      最近更新 更多