我想在UWP应用程序中将按钮文本从"Run“更改为"Stop”并向后更改,并更改click flag的布尔属性。我尝试使用微软的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}"> <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();