gpt4 book ai didi

c# - 用户控制事件不触发

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:23 28 4
gpt4 key购买 nike

我在 UserControl 中的事件有问题。

<UserControl x:Class="LatikeManager.Views.RoomManager_Item"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LatikeManager.Views"
mc:Ignorable="d"
d:DesignHeight="170" d:DesignWidth="220" Width="200" Height="150" Background="Transparent" MouseLeftButtonUp="UserControl_MouseLeftButtonUp">

<!--<Border BorderBrush="{Binding BorderColor}" BorderThickness="2" Grid.Row="0" Grid.RowSpan="2" VerticalAlignment="Top">-->

<Grid Name="gdContent" Width="200" Height="150" Background="Wheat">

<Button Width="30" Height="30"></Button>

</Grid>
<!--</Border>-->

背后的代码:

public partial class RoomManager_Item : UserControl, INotifyPropertyChanged
{
public class MyEventArgs : EventArgs
{
public bool RoomManagerItemEventArgs { get; set; }

public MyEventArgs(bool isSelected)
{
RoomManagerItemEventArgs = isSelected;
}
}

public class OnlineEventArgs : EventArgs
{
public bool OnlineStateChanged { get; set; }

public OnlineEventArgs(bool Connected)
{
OnlineStateChanged = Connected;
}
}

#region //Events
public delegate void MyEventHandler(object sender, MyEventArgs args);
public event MyEventHandler RoomManagerItem_SelectedChanged;

public delegate void OnlineEventHandler(object sender, OnlineEventArgs args);
public event OnlineEventHandler RoomManagerItem_OnlineChanged;

public event PropertyChangedEventHandler PropertyChanged;
#endregion

#region //Images
private dynamic unknownStatus = new BitmapImage(new Uri("Resources/Help-48.png", UriKind.Relative));
private dynamic greenStatus = new BitmapImage(new Uri("Resources/Ok-48.png", UriKind.Relative));
private dynamic warningStatus = new BitmapImage(new Uri("Resources/Error-48.png", UriKind.Relative));
private dynamic errorStatus = new BitmapImage(new Uri("Resources/Cancel-48.png", UriKind.Relative));

private dynamic computerOnline = new BitmapImage(new Uri("Resources/Workstation-50(2).png", UriKind.Relative));
private dynamic computerOffline = new BitmapImage(new Uri("Resources/Workstation-50.png", UriKind.Relative));

private dynamic laptopOnline = new BitmapImage(new Uri("Resources/Workstation-50(2).png", UriKind.Relative));
private dynamic laptopOffline = new BitmapImage(new Uri("Resources/Laptop-50.png", UriKind.Relative));

public int wert = 0;
#endregion

#region
private ILatikeClient _ClientRef;
public ILatikeClient ClientRef
{
get { return _ClientRef; }
set
{
_ClientRef = value;
if (RoomManagerItem_OnlineChanged != null)
RoomManagerItem_OnlineChanged(this, new OnlineEventArgs(IsConnected));
}
}

public bool IsConnected { get { return ClientRef != null; } }
public RegisterInfo Info { get; set; }
public List<Program> ProgramInfo { get; set; }
public HardwareInfo HardwareInfo { get; set; }
public dynamic SSDStateSource { get; set; }
public DB.SYSTEM Rechner { get; set; }
public dynamic ComputerOnlinePicture { get; set; }

private bool _IsSelected;
public bool IsSelected
{
get { return _IsSelected; }
set
{
_IsSelected = value;
}
}

public RoomManager_Item(DB.SYSTEM rechner)
{
Rechner = rechner;
InitializeComponent();
DataContext = this;
}

private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
IsSelected = !IsSelected;
}
else
{
IsSelected = true;
}

if (RoomManagerItem_SelectedChanged != null)
{
RoomManagerItem_SelectedChanged(this, new MyEventArgs(IsSelected));
}
}

}

我不知道为什么这不起作用,它没有在用户控件中的任何地方触发。没有事件会触发,即使是按钮的点击事件也不会触发,按钮也不会悬停。

在我的代码中有一个调度程序,它工作正常

new DispatcherTimer(TimeSpan.FromSeconds(10), DispatcherPriority.Normal,
(s, a) =>
{
RefreshClientInfo(null, null);
},
Dispatcher);

...但我无法以任何方式与该表单进行交互。

此外还有将 UC 加载到我的网格中的代码

private void Room_Changed(object sender, SelectionChangedEventArgs e)
{
if (ctxROOM.Count != 0)
if (cbRooms.SelectedIndex != -1)
if (CurrentRoom == "Übersicht")
{
btnAllNoneEnabled = false;
gdDesign.RowDefinitions.Clear();
gdDesign.ColumnDefinitions.Clear();
gdDesign.Children.Clear();
gdContent.Children.Clear();
var overviewRoom = ctxROOM.Select(p => p).ToList();//get all rooms
//checked any room on content
foreach (DB.ROOM item in overviewRoom)
{
var overviewSystemRoom = ctxSYSTEM_ROOM.Where(p => p.room_name == item.room_name).ToList();
//if the room has Systems, create them
foreach (var checkContent in overviewSystemRoom)
if (checkContent != null)
{
gdContent.Children.Add(new RoomManager_RoomOverview(item, overviewSystemRoom, ctxSYSTEM));
break;
}
}
}
else
{
btnAllNoneEnabled = true;
gdContent.Children.Clear();
var raum = ctxROOM.Single(p => p.room_name == CurrentRoom);
gdDesign.RowDefinitions.Clear();
for (int i = 0; i < raum.room_rows; i++)
{
gdDesign.RowDefinitions.Add(new RowDefinition()
{
Height = new GridLength(1, GridUnitType.Star),
MinHeight = 152
});
}
gdDesign.ColumnDefinitions.Clear();
for (int i = 0; i < raum.room_columns; i++)
{
gdDesign.ColumnDefinitions.Add(new ColumnDefinition()
{
Width = new GridLength(1, GridUnitType.Star),
MinWidth = 202
});
}

gdDesign.Children.Clear();
for (int r = 0; r < raum.room_rows; r++)
{
for (int c = 0; c < raum.room_columns; c++)
{
RoomManager_Item item;
var system = ctxSYSTEM_ROOM.SingleOrDefault(p => p.row == r && p.column == c && p.room_name == CurrentRoom);
DB.SYSTEM systemVar = new DB.SYSTEM();

if (system != null)
{
systemVar = ctxSYSTEM.SingleOrDefault(p => p.description == system.system_description);
item = new RoomManager_Item(systemVar);
item.ContextMenu = ctxMenue;
item.RoomManagerItem_SelectedChanged += (s, b) =>
{
//var roomSeats = _Ctx.ROOM.Select(p => p.room_name == CurrentRoom ? p.seats : 0);
var selectedSystem = ctxSYSTEM_ROOM.SingleOrDefault(p => p.system_description == systemVar.description);
roomItemsSelected[selectedSystem.row, selectedSystem.column] = b.RoomManagerItemEventArgs;
if (b.RoomManagerItemEventArgs) count++;
else count--;
PropertyChanged(this, new PropertyChangedEventArgs("btnAllNone"));
};
gdDesign.Children.Add(item);
Grid.SetRow(item, r);
Grid.SetColumn(item, c);
}
}
};
}
}

这是我获取我需要的 UserControl 数量并将它们添加到我的网格的方法。

最佳答案

听起来很明显有什么东西在阻止或处理鼠标事件。我强烈建议你下载Snoop for WPF这将允许您询问 UI 以查看是否有任何内容超出您的控制范围,甚至可以监视鼠标事件以查看正在处理它们的内容。

关于c# - 用户控制事件不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38894062/

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