Ciao Ricardo,
non so se nel frattempo hai risolto, comunque nel caso ti dico come ho risolto io.
Nel caso in questione di una label, una valida ipotesi potrebbe essere quella di una dependency property e non di un binding diretto.
Si tratta di scrivere un po' di codice, ma tutto sommato può valerne la pena.
Ti posto la mia soluzione così come l'ho realizzata per verificare la tua necessità, fammi sapere se risolve il tuo problema.
Questa la parte
xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" IsTabStop="False">
<Grid>
<Label Height="28" HorizontalAlignment="Left" Margin="12,218,0,0" Name="label1" VerticalAlignment="Top" Width="198" />
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" />
</Grid>
</Window>
questa la parte
codice
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;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Dependency Property
public static readonly DependencyProperty MySetCountProperty =
DependencyProperty.Register("MySetCount", typeof(Int32), typeof(MainWindow),
new FrameworkPropertyMetadata(0, OnMySetCountPropertyChanged));
// Proprietà "wrapper" per la Dependency Property
public int MySetCount
{
get { return (Int32)GetValue(MySetCountProperty); }
set { SetValue(MySetCountProperty, value); }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// il mio "set" di dati
var mySet = new[]{
new {id=1, description="test_1"},
new {id=2, description="test_2"},
new {id=3, description="test_3"}
};
// bindo il mio set di dati
dataGrid1.ItemsSource = mySet;
// valorizzo la proprietà "wrapper"
MySetCount = mySet.Count();
}
/// <summary>
/// Gestore dell'evento di "changed" della Dependency Property
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private static void OnMySetCountPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
// trovo la "MainWindow" in cui si è verificato l'evento
MainWindow mainWindow = (MainWindow)source;
// trovo la label da "bindare"
object objLabel = mainWindow.FindName("label1");
// se la trovo ne valorizzo il content con il valore assegnato alla property
if (objLabel != null)
{
((Label)objLabel).Content = ((Int32)e.NewValue).ToString();
}
}
}
}
HTH,
Roberto
Modificato da dancerjude il 22 agosto 2011 23.09 -