gpt4 book ai didi

wpf - 将两个命令绑定(bind)到一个按钮?

转载 作者:行者123 更新时间:2023-12-04 15:53:28 24 4
gpt4 key购买 nike

我有一个选项卡控件,其中包含两个选项卡,一个向我显示正在运行的进程的消息,另一个向我显示一个网页!

我有三个按钮(开始、停止和清除)
当我按下开始时,进程运行并显示消息。

我需要的是当我按下开始按钮时,两个选项卡都会向我显示所需的内容。这意味着我需要两个将两个命令绑定(bind)到同一个按钮

你知道如何为 1 个按钮绑定(bind)两个命令吗?

我正在使用 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

最佳答案

你可以像这样调用多个命令:

// 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 包。

关于wpf - 将两个命令绑定(bind)到一个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22803913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com