用户控制事件不会触发

时间:2016-08-11 10:40:04

标签: c# wpf events

我在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数量并将其添加到我的网格的方法。

1 个答案:

答案 0 :(得分:1)

听起来有明显的东西阻挡或处理鼠标事件。我强烈建议您下载Snoop for WPF这将允许您查询用户界面以查看是否有任何内容超出您的控件,甚至监控鼠标事件以查看处理它们的内容。