我有一个标签控件包含两个标签,一个显示正在运行的进程的消息,另一个向我显示一个网页!
我有三个按钮(开始,停止和清除) 当我按下开始进程并显示消息时。
我需要的是按下“开始”按钮,两个标签显示所需的内容。这意味着我需要两个绑定两个命令到同一个按钮
你知道如何绑定1个按钮两个命令吗?
我正在使用MVVM,而且我是MVVM的新手。
<Button Content="Start" Command="{Binding StartProcess_Command }" Height="25" Width="60" Grid.Row="0" Grid.Column="0" Margin="10"/>
<Button Content="Stop" Command="{Binding StopProcess_Command}" Height="25" Width="60" Grid.Row="0" Grid.Column="1" Margin="10"/>
<Button Content="Clear" Command="{Binding ClearBtn_Command}" Height="25" Width="60" Grid.Row="0" Grid.Column="2" Margin="10"/>
<telerik:RadTabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="1" Grid.ColumnSpan="4" Margin="10">
<telerik:RadTabItem Header="Maestro" IsSelected="{Binding Path=MaestroSelected}">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<local:WPFBrowser/>
</ScrollViewer>
</telerik:RadTabItem>
<telerik:RadTabItem Header="Messages">
<Grid Background="LightGray">
<ScrollViewer VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
<TextBlock TextWrapping="Wrap" Text="{Binding Path=output_process,UpdateSourceTrigger=Default}"/>
</ScrollViewer>
</Grid>
</telerik:RadTabItem>
</telerik:RadTabControl >
这是WPFBrowser.cs代码:
public WPFBrowser()
{
ServerString = "localhost"; //defualt value
PortString = "9999"; //default value
this.refreshCommand = new Lunch_Maestro.ViewModel.Command(DoRefreshCommand);
m_WPFBrowser = new WebBrowser()
{
HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch,
VerticalAlignment = System.Windows.VerticalAlignment.Stretch
};
m_WPFBrowser.Navigated += WPFBrowser_Navigated;
this.Content = m_WPFBrowser;
}
private void WPFBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
this.HideScriptErrors(m_WPFBrowser, true);
}
/// <summary>
/// Found this link with interesting scenerios for script erros...
/// http://social.msdn.microsoft.com/Forums/vstudio/en-US/4f686de1-8884-4a8d-8ec5-ae4eff8ce6db/new-wpf-webbrowser-how-do-i-suppress-script-errors?forum=wpf
/// </summary>
public void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fiComWebBrowser == null) return;
object objComWebBrowser = fiComWebBrowser.GetValue(wb);
if (objComWebBrowser == null) return;
objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { Hide });
}
private void DoRefreshCommand()
{
m_WPFBrowser.Navigate(new Uri("http://" + ServerString + ":" + PortString + "/"));
}
private bool _maestroSelected;
public bool MaestroSelected
{
get { return _maestroSelected; }
set
{
_maestroSelected = value;
if (_maestroSelected == true)
m_WPFBrowser.Navigate(new Uri("http://" + ServerString + ":" + PortString + "/"));
OnPropertyChanged("MaestroSelected");
}
}
private readonly WebBrowser m_WPFBrowser;
private string _serverString;
public string ServerString
{
get { return _serverString; }
set
{
_serverString = value;
OnPropertyChanged("ServerString");
}
}
private string _portString;
public string PortString
{
get { return _portString; }
set
{
_portString = value;
OnPropertyChanged("PortString");
}
}
private Lunch_Maestro.ViewModel.Command refreshCommand;
public Lunch_Maestro.ViewModel.Command RefreshCommand
{
get { return refreshCommand; }
}
// and Inotify property changes here
答案 0 :(得分:6)
你想点击一个Button
并运行两段代码......这听起来并不复杂。使用其中一个可用的RelayCommand
s,问题可以修复为:
public ICommand SomeCommand
{
get { return new ActionCommand(action) => { RunFirstFunction(action);
RunSecondFunction(action) }, canExecute => someCondition); }
}
在XAML中:
<Button Command="{Binding SomeCommand}" CommandParameter="{Binding SomeObject}">
Click Me
</Button>
ActionCommand
是我自己的RelayCommand
形式,但所有delegate ICommand
实现大致相同,可以这样工作。 CommandParameter
值作为action
变量进入代码,并传递给两个函数。
答案 1 :(得分:3)
您可以使用CompositeCommand框架中的Prism。 在viewmodel中创建一个额外的CompositeCommand,使用此CompositeCommand注册两个普通命令,并将该按钮绑定到CompositeCommand。
请参阅第130页的Prism documentation,了解如何使用CompositeCommands。
您的应用需要引用Prism包中包含的Microsoft.Practices.Prism.dll。
答案 2 :(得分:2)
你可以像这样调用多个命令:
// xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<Button>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding Cmd1}" />
<i:InvokeCommandAction Command="{Binding Cmd2}" />
<i:InvokeCommandAction Command="{Binding Cmd3}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
这也将允许您在需要时控制命令执行顺序
PS:您只需要安装 Microsoft.Xaml.Behaviors.Wpf
NuGet 包。