View Full Version : [WPF] UserControl: usare gli elementi definito in un altro UserControl
robertino_salemi
11-03-2015, 17:32
Ciao a tutit,
nella mia applicazione ho incluso un UserControl "B" in un UserControl "A".... so, I vorrei richiamare il datagrid "dgPlan" definito in UserControl "A" dall'UserControl "B".
Come faccio a richiamarlo?
Grazie.
[Kendall]
11-03-2015, 17:39
Ciao a tutit,
nella mia applicazione ho incluso un UserControl "B" in un UserControl "A".... so, I vorrei richiamare il datagrid "dgPlan" definito in UserControl "A" dall'UserControl "B".
Come faccio a richiamarlo?
Grazie.
Definisci "richiamarlo".
Posta anche il codice xaml e il listato c# dei due componenti, cosė riusciamo ad aiutarti meglio.
robertino_salemi
12-03-2015, 00:12
Ok, domani lo posto.
Dovrei semplicemente richiamare il DataGrid dell'userControl A nell'UserControl B al fine di aggiornare i dati da visualizzare.
Grazie.
robertino_salemi
12-03-2015, 10:12
Buongiorno,
ecco i miei due UserControl.
UserControlA
<UserControl x:Class="MyProject.Views.UserControlA"
x:Name="UserControlUserControlA"
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:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:staticData="clr-namespace:MyProject.Views"
mc:Ignorable="d"
d:DesignHeight="381" d:DesignWidth="1200" Unloaded="UserControl_Unloaded">
<StackPanel Grid.Row="1">
<ScrollViewer>
<staticData:UserControlB />
</ScrollViewer>
</StackPanel>
<DataGrid Name="dgSample" ItemsSource="{Binding}" AutoGenerateColumns="False"
AlternatingRowBackground="{StaticResource RowAlternateColorBackground}"
GridLinesVisibility="All" VerticalGridLinesBrush="{StaticResource GridLineColor}"
HorizontalGridLinesBrush="{StaticResource GridLineColor}"
IsReadOnly="False" CanUserAddRows="False">
[MY_COLUMNS]
</DataGrid>
</UserControl>
UserControlB
<UserControl x:Class="MyProject.Views.UserControlB"
x:Name="UserControlUserControlB"
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:mui="http://firstfloorsoftware.com/ModernUI"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:staticData="clr-namespace:MyProject.Views"
mc:Ignorable="d"
d:DesignHeight="381" d:DesignWidth="1200">
[MY_CONTENT]
<!-- Search Button -->
<Button Margin="15,0,0,0" Name="bntSearch" Content="{StaticResource menuSeachButton}" Click="bntSearch_Click"></Button>
</Grid>
</UserControl>
dgPlan č il DataGrid in UserControlA.
Quando l'utente esegui il click su bntSearch button in UserControlB, dgPlan deve essere aggiornata con i nuovi dati...
Grazie.
robertino_salemi
12-03-2015, 12:54
Risolto grazie ad un utente:
nel mio UserControlA:
public void RefreshDataGrid(IEnumerable itemsSource)
{
dgPlan.ItemsSource = itemsSource;
}
nel mio UserControlB:
void bntSearch_Click(object sender, RoutedEventArgs e)
{
MyProject.Views.UserControlA uc = FindParent<MyProject.Views.UserControlA>(this);
if (uc != null)
{
uc.RefreshDataGrid(myCollection);
}
}
private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindParent<T>(parent);
}
Grazie! ;)
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.