我是wpf和xaml(一般来说是Windows开发)的新手,我的背景是asp.net,在那之前是经典的asp。我正在处理一个应用程序,需要在处理过程中禁用/灰显按钮,并在此处阅读帖子以执行以下操作,但它似乎不起作用。有人会帮助我解决我错过的问题吗?
<Window x:Class="SCGen.Application.LoadForecast.EngineExecution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igEditors="http://infragistics.com/Editors"
SizeToContent="WidthAndHeight"
Title="Engine Execution"
ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="myStyle" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Command" Value="{Binding ExecuteEngine}" />
<Setter Property="Content" Value="Execute Engine" />
<Style.Triggers>
<Trigger Property="Command" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Border Padding="8">
<StackPanel>
<StackPanel MaxWidth="200" HorizontalAlignment="Left">
<TextBlock Text="Select Forecast Engine" TextAlignment="Center" FontSize="13" />
<igEditors:XamComboEditor ItemsSource="{Binding ForecastEngines}" SelectedItem="{Binding SelectedEngine}" Margin="0,5" />
<Button Style="{StaticResource ResourceKey=myStyle}" />
</StackPanel>
<TextBlock Text="{Binding EngineStatus}" FontSize="15" FontStyle="Italic" Margin="0,14" Width="400" TextWrapping="Wrap" />
</StackPanel>
</Border>
谢谢
我已将xaml更改为以下内容:
<Button Content="Execute Weather Import" Command="{Binding ExecuteWeather}" Style="{StaticResource ButtonStyle}" IsEnabled="{Binding IsEnabled}"/>
在ViewModel中,我有以下内容:
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set { _isEnabled = value; }
}
我在这里设置了_isEnabled:
private string LaunchWeatherImport(string strVendor)
{
_isEnabled = false;
string uri = ConfigurationManager.AppSettings["ManualExecutionFacilitatorService"];
ClientConnectionInfo connection = new ClientConnectionInfo(uri) { UseSecurity = true };
connection.SetTimeouts();
Logger.LogInfo("Calling Facilitator service to manually import " + strVendor + " weather data.");
((NetTcpBinding)connection.Binding).Security.Mode = System.ServiceModel.SecurityMode.None;
using (var client = new FacilitatorManualExecutionClient(connection))
{
client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(int.Parse(ConfigurationManager.AppSettings["OperationTimeOutMinutes"]));
try
{
_isEnabled = true;
return "success";
// uncomment this line before commit
//return client.ExecuteWeather(strVendor);
}
#region catch
catch (Exception ex)
{
Logger.LogError(ex.Message, ex);
return ex.Message;
}
#endregion
}
}
我仍然无法正常工作。很抱歉不得不添加到此,但评论的回复字段不足以发布代码。
答案 0 :(得分:2)
对于初学者,您在Command属性上设置了触发器,但您没有为该按钮设置该属性的绑定:
<Button Style="{StaticResource ResourceKey=myStyle}" />
应该是:
<Button Style="{StaticResource ResourceKey=myStyle}" Command="{Binding MyCommand}" />
[其中MyCommand是您绑定的实际命令的名称]
我不确定它是否会起作用,因为当Command属性为null时触发器设置为触发,但是如果绑定到命令属性,则遵循MVVM模式,那么命令属性不应为null所以触发器也不会触发。
更新:
您需要在您的类上实现具有该属性的INotifyPropertyChanged接口。
public class MyClass : System.ComponentModel.INotifyPropertyChanged
然后添加实现:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
然后将您的属性更改为:
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
NotifyPropertyChanged("IsEnabled");
}
}