我正在使用 telerik:RadGridView 和 telerik:RadContextMenu.ContextMenu 在我的应用程序中生成右键菜单。我需要能够从所选行中获取服务器名称和会话 ID,以便传递给 Disconnect 和 Logoff 函数。但是我很难获取我需要的数据。
这是组件的 XAML
<telerik:RadGridView x:Name="UserSessionGrid" IsReadOnly="True" FontWeight="Bold" AutoGeneratingColumn="UserSessionGrid_AutoGeneratingColumn" CanUserResizeColumns="False" CanUserDeleteRows="False" CanUserResizeRows="False" ClipboardCopyMode="All" Copied="UserSessionGrid_Copied" >
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu Opened="RadContextMenu_Opened" ItemClick="RadContextMenu_ItemClick">
<telerik:RadContextMenu.Items>
<telerik:RadMenuItem Header="Copy" />
<telerik:RadMenuItem Header="Disconnect" />
<telerik:RadMenuItem Header="Logoff" />
</telerik:RadContextMenu.Items>
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</telerik:RadGridView>
这是处理右键单击的相关代码
/// <summary>
/// Handles the ItemClick event of the RadContextMenu control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.Windows.RadRoutedEventArgs"/> instance containing the event data.</param>
private void RadContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
RadContextMenu menu = (RadContextMenu)sender;
RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
GridViewRow row = menu.GetClickedElement<GridViewRow>();
GridViewCell cell = menu.GetClickedElement<GridViewCell>();
GridViewRowItem rowitem = menu.GetClickedElement<GridViewRowItem>();
if (clickedItem != null && row != null)
{
string header = Convert.ToString(clickedItem.Header);
switch (header)
{
case "Copy":
Clipboard.SetText(cell.Value.ToString());
break;
case "Disconnect":
// Grab Server Name Column and Session ID Column Data
break;
case "Logoff":
// Grab Server Name Column and Session ID Column Data
break;
default:
break;
}
}
}
/// <summary>
/// Handles the Opened event of the RadContextMenu control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
RadContextMenu menu = (RadContextMenu)sender;
GridViewRow row = menu.GetClickedElement<GridViewRow>();
if (row != null)
{
row.IsSelected = row.IsCurrent = true;
GridViewCell cell = menu.GetClickedElement<GridViewCell>();
if (cell != null)
{
cell.IsCurrent = true;
}
}
else
{
menu.IsOpen = false;
}
}
这是我的课程
class Session
{
public String Server { get; set; }
public String Domain { get; set; }
public String User { get; set; }
public int sID { get; set; }
public ConnectionState State { get; set; }
public IPAddress IP { get; set; }
public String Workstation { get; set; }
public DateTime? Connect { get; set; }
public DateTime? Login { get; set; }
public TimeSpan Idle { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Session"/> class.
/// </summary>
/// <param name="server">The server.</param>
/// <param name="domain">The domain.</param>
/// <param name="user">The user.</param>
/// <param name="session">The session.</param>
/// <param name="state">The state.</param>
/// <param name="ip">The ip.</param>
/// <param name="workstation">The workstation.</param>
/// <param name="connect">The connect.</param>
/// <param name="login">The login.</param>
/// <param name="idle">The idle.</param>
public Session (string server, string domain, string user, int session, ConnectionState state, IPAddress ip, string workstation, DateTime? connect, DateTime? login, TimeSpan idle)
{
this.Server = server.ToUpper();
this.Domain = domain.ToUpper();
this.User = user;
this.sID = session;
this.State = state;
this.IP = ip;
this.Workstation = workstation.ToUpper();
this.Connect = connect;
this.Login = login;
this.Idle = idle;
}
}
使用以下代码调用
/// <summary>
/// Handles the DoWork event of the worker control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DoWorkEventArgs"/> instance containing the event data.</param>
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
App.Current.Dispatcher.Invoke((Action)delegate
{
UserSessionGrid.IsBusy = true;
});
ITerminalServicesManager manager = new TerminalServicesManager();
foreach (var ServerName in ServerList)
{
using (ITerminalServer server = manager.GetRemoteServer(ServerName))
{
try
{
server.Open();
foreach (ITerminalServicesSession session in server.GetSessions())
{
items.Add(new Session(server.ServerName, session.DomainName, session.UserName, session.SessionId, session.ConnectionState, session.ClientIPAddress, session.WindowStationName, session.ConnectTime,session.LoginTime, session.IdleTime));
//worker.ReportProgress(session.SessionId);
}
server.Close();
}
catch (Win32Exception) { }
catch (SystemException) { }
catch (Exception) { }
}
}
}