【发布时间】: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 行为中有错误,但我不知道在哪里......
【问题讨论】: