Torna al Thread
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Markup;
using Prova;
using System.Data;
using System.ComponentModel;
namespace ClientSupervisore2.LayoutControls
{
/// <summary>
/// Logica di interazione per ibrido.xaml
/// </summary>
///
public partial class ibrido2 : UserControl, INotifyPropertyChanged
{
//Evento per segnalare la modifica delle proprietà per il databinding
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
public static readonly DependencyProperty ResultsProperty = DependencyProperty.Register("Results", typeof(DataView), typeof(ibrido2));
public DataView Results
{
get { return (DataView)GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
public ibrido2()
{
//Caricamento Xaml da file esterno
LoadXAMLMethod();
//Aggiunta evento load al controllo
this.Loaded +=new RoutedEventHandler(ibrido2_Loaded);
}
Button ButtoninXAML;
TextBox TextBox1inXAML;
DependencyObject rootObject;
#region LoadXAML
public void LoadXAMLMethod()
{
try
{
StreamReader mysr = new StreamReader("C:\\Progetti\\Visual Studio\\ClientSupervisore2\\ClientSupervisore2\\bin\\Debug\\ibrido.xaml");
rootObject = XamlReader.Load(mysr.BaseStream) as DependencyObject;
ButtoninXAML = LogicalTreeHelper.FindLogicalNode(rootObject, "button1") as Button;
ButtoninXAML.Click += new RoutedEventHandler(Button_Click);
TextBox1inXAML = LogicalTreeHelper.FindLogicalNode(rootObject, "textBox1") as TextBox;
this.Content = rootObject;
Resources.Source = new Uri("C:\\Progetti\\Visual Studio\\ClientSupervisore2\\ClientSupervisore2\\bin\\Debug\\ibrido_Dict.xaml");
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
#endregion
//Controllo caricato
private void ibrido2_Loaded(Object sender, RoutedEventArgs e)
{
//Le prossime operazioni vengono effettuate su evento loaded perchè se no il
//visualtreehelper non ritorna nulla
//Esempio di come ritirare tutte le texbox dal controllo:
IEnumerable<TextBox> ListaTextBox = FindVisualChildren<TextBox>(rootObject);
TextBox orig=new TextBox();
TextBox dest=new TextBox();
foreach (TextBox Tb in ListaTextBox)
{
if (Tb.Name == "textBox1")
{
orig = Tb;
}
if (Tb.Name == "textBox2")
{
TextBox1inXAML = Tb;
}
}
}
#region findvisualchilden
//La seguente restituisce tutti gli oggetti visuali di un determinato tipo
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
#endregion
//Esempio di gestione eventi trasportati da xaml
public void Button_Click(object sender, RoutedEventArgs e)
{
Binding dataViewBinding = new Binding();
dataViewBinding.Source = Results;
dataViewBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
dataViewBinding.Mode = BindingMode.TwoWay;
dataViewBinding.Path = new PropertyPath("[0][value]");
TextBox1inXAML.DataContext = this.Results;
TextBox1inXAML.SetBinding(TextBox.TextProperty, dataViewBinding);
}
}
}