-
- MarioBarbero
- Posts: 30
- Joined:
Events in WPF using Unity
We have a project using WPF, Unity and bote wirh NoesisGUI.0
When I want to debug my cs code, I execute Unity and then attach WPF project to Unity.
I have two issues with a UserControl (cs file and xaml file):
- The first, I have a comboBox control in xaml code. This comboBox has a event like this way (Loaded="cmbControl_Loaded"). I dont know why, but never execute this event. Is this way correct to use with Noesis or exist another better way?
- The second, another problem with the same comboBox control. I try to get control in cs file. This way (this.FindName("cmbControl") as ComboBox. This metros, "FindName" always return null with this control and others. Do you know why happen this? What is the correct way to get control in code behind?
Thanks
When I want to debug my cs code, I execute Unity and then attach WPF project to Unity.
I have two issues with a UserControl (cs file and xaml file):
- The first, I have a comboBox control in xaml code. This comboBox has a event like this way (Loaded="cmbControl_Loaded"). I dont know why, but never execute this event. Is this way correct to use with Noesis or exist another better way?
- The second, another problem with the same comboBox control. I try to get control in cs file. This way (this.FindName("cmbControl") as ComboBox. This metros, "FindName" always return null with this control and others. Do you know why happen this? What is the correct way to get control in code behind?
Code: Select all
<Page x:Class="BKDashboardTemp.ComboBoxCustom"
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:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:Assets.BKCycling.Scripts.ViewController.ViewModel.Generic.ComboBox"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" x:Name="vComboBoxCustom">
<ComboBox x:Name="cmbControl" ItemsSource="{Binding ItemsSource, ElementName=vComboBoxCustom}"
DisplayMemberPath="{Binding DisplayMember, ElementName=vComboBoxCustom}"
SelectedValuePath ="{Binding ValueMember, ElementName=vComboBoxCustom}"
Loaded="cmbControl_Loaded">
<b:Interaction.Triggers>
<b:EventTrigger EventName="SelectionChanged">
<b:InvokeCommandAction Command="{Binding SelectedItemChangedCommand, ElementName=vComboBoxCustom}"/>
</b:EventTrigger>
</b:Interaction.Triggers>
</ComboBox>
</Page>Code: Select all
#define NOESIS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Noesis;
namespace BKDashboardTemp
{
/// <summary>
/// Lógica de interacción para ComboBoxCustom.xaml
/// </summary>
public partial class ComboBoxCustom : Page
{
//public NoesisEventCommand SelectedItemChangedCommand { get; set; } = new NoesisEventCommand();
private ComboBox _cmbControl;
public NoesisEventCommand LoadedCommand { get; set; } = new NoesisEventCommand();
public object ItemsSource
{
get { return (object)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(nameof(ItemsSource), typeof(object), typeof(ComboBoxCustom), new PropertyMetadata(null));
public string DisplayMember
{
get { return (string)GetValue(DisplayMemberProperty); }
set { SetValue(DisplayMemberProperty, value); }
}
public static readonly DependencyProperty DisplayMemberProperty =
DependencyProperty.Register(nameof(DisplayMember), typeof(string), typeof(ComboBoxCustom), new PropertyMetadata(default(string)));
public string ValueMember
{
get { return (string)GetValue(ValueMemberProperty); }
set { SetValue(ValueMemberProperty, value); }
}
public static readonly DependencyProperty ValueMemberProperty =
DependencyProperty.Register(nameof(ValueMember), typeof(string), typeof(ComboBoxCustom), new PropertyMetadata(default(string)));
public NoesisEventCommand SelectedItemChangedCommand
{
get { return (NoesisEventCommand)GetValue(SelectedItemChangedCommandProperty); }
set { SetValue(SelectedItemChangedCommandProperty, value); }
}
public static readonly DependencyProperty SelectedItemChangedCommandProperty =
DependencyProperty.Register(nameof(SelectedItemChangedCommand), typeof(NoesisEventCommand), typeof(ComboBoxCustom), new PropertyMetadata(default(NoesisEventCommand)));
public ComboBoxCustom()
{
InitializeComponent();
}
#if NOESIS
private void InitializeComponent()
{
NoesisUnity.LoadComponent(this);
LoadedCommand.AddListener(ButtonClicker);
}
private void ButtonClicker(object arg0)
{
}
private void cmbControl_Loaded(object sender, RoutedEventArgs e)
{
if (sender is ComboBox cmb && cmb != null)
_cmbControl = cmb;
}
public ItemCollection GetItems()
{
ComboBox cmb = this.FindName("cmbControl") as ComboBox;
return _cmbControl?.Items;
}
// public static T FindChild<T>(DependencyObject parent, string childName)
//where T : DependencyObject
// {
// // Confirm parent and childName are valid.
// if (parent == null)
// {
// return null;
// }
// T foundChild = null;
// int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
// for (int i = 0; i < childrenCount; i++)
// {
// DependencyObject child = VisualTreeHelper.GetChild(parent, i);
// // If the child is not of the request child type child
// var childType = child as T;
// if (childType == null)
// {
// // recursively drill down the tree
// foundChild = FindChild<T>(child, childName);
// // If the child is found, break so we do not overwrite the found child.
// if (foundChild != null)
// {
// break;
// }
// }
// else if (!string.IsNullOrEmpty(childName))
// {
// var frameworkElement = child as FrameworkElement;
// // If the child's name is set for search
// if (frameworkElement != null && frameworkElement.Name == childName)
// {
// // if the child's name is of the request name
// foundChild = (T)child;
// break;
// }
// // Need this in case the element we want is nested
// // in another element of the same type
// foundChild = FindChild<T>(child, childName);
// }
// else
// {
// // child element found.
// foundChild = (T)child;
// break;
// }
// }
// return foundChild;
// }
#endif
}
}-
-
sfernandez
Site Admin
- Posts: 3364
- Joined:
Re: Events in WPF using Unity
1) As explained in the Events tutorial, when using code-behind events you need to connect your control to the event:
2) To call FindName you have to wait until the xaml is loaded in the InitializeComponent. This happens when you call NoesisUnity.LoadComponent(this). So you can do something like this:
Please let me know if you find any trouble with this.
Code: Select all
public partial class ComboBoxCustom : Page
{
protected override bool ConnectEvent(object source, string eventName, string handlerName)
{
if (eventName == "Loaded" && handlerName == "cmbControl_Loaded")
{
((ComboBox)source).Loaded += this.cmbControl_Loaded;
return true;
}
return false;
}
...
}Code: Select all
private void InitializeComponent()
{
NoesisUnity.LoadComponent(this);
_cmbControl = (ComboBox)FindName("cmbControl");
LoadedCommand.AddListener(ButtonClicker);
}-
-
sfernandez
Site Admin
- Posts: 3364
- Joined:
Re: Events in WPF using Unity
Great, glad to be helpful.
Who is online
Users browsing this forum: Google [Bot] and 4 guests