vendredi 6 février 2015

Listbox itemsource as User control


Vote count:

0




I have a user control which has few controls.



<UserControl>
<Grid>
<Expander Header="{Binding Path=Headerval}">
<StackPanel Margin="10,4,0,0">
<DataGrid
x:Name="dataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=records}"/>
</StackPanel>
</Expander>
</Grid>
</UserControl>


I am using this set of user controls as my itemsource in my Listbox



<ListBox x:Name="myListBox" ItemsSource="{Binding Path=_myControl}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<NewWPFApp:MyUserControl />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>

</ListBox>


and in my Mainwindow Code



public ObservableCollection<GetControlsList> _myControl { get; set; }

_myControl = new ObservableCollection<GetControlsList>();
_myControl.Add(new GetControlsList(new MyUserControl()));

public class GetControlsList
{
public ObservableCollection<MyUserControl> _ListControls = new ObservableCollection<MyUserControl>();
public GetControlsList(params MyUserControl[] controls)
{
foreach (var control in controls)
{
_ListControls.Add(control);
}
}
public ObservableCollection<MyUserControl> ListCOntrols
{
get { return _ListControls; }
}
}


However here Initializecomponent of MyUserControl is called twice


What can I do about it ??


Thanks



asked 1 min ago

kyle

3,024






Listbox itemsource as User control

Aucun commentaire:

Enregistrer un commentaire