我有一个按钮,该操作可能需要很多时间。这是一个异步方法,因此,我不会阻塞UI,但是我想禁用该按钮,以避免执行其他相同操作的机会。
为此,我禁用了该按钮,并更改了其背景,以显示正在运行的操作是单击该按钮时启动的操作。
我有一个转换器,因为在我的视图模型中,我有一个bool属性,该属性设置操作是开始还是操作完成。要将布尔转换为背景色,请使用转换器。
好吧,问题在于,如果我禁用了该按钮,则无法使用转换器设置背景。
我想知道如何做到这一点,禁用按钮并使用转换器更改背景。
我的视图模型中的代码
private async void componentesAsignar()
{
Bussy = true; //need for the converter, to set the background
MyActionStarted = true; //need to disabled the button
await Task.Delay(10000); //the code that takes many time
}
我认为我的代码
<Button Content="MyAction" Height="23" Name="btnMyAction" Width="75"
Background="{Binding MyActionStarted, Converter={StaticResource actionInProgressToColorConverter}}">
我的转换器
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(((bool)value) == false)
{
return "#FFDDDDDD";
}
else
{
return "#FF589FF3";
}
}
非常感谢。
代替使用转换器,我将使用DataTrigger:
<Button ...>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FFDDDDDD"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Bussy, Mode=OneWay}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Background" Value="#FF589FF3"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句