简体   繁体   中英

Usercontrol as DataTemplate no DataContext (null)

I got the problem that my DataTemplate (UserControl) has no DataContext. But I need the DataContext because I got a Drop-Event where I call the ViewModel.

I got a ItemsControl with an ItemTemplateSelector="{StaticResource PropertyDataTemplateSelector}"> where I load between two different UserControls.

When I set my breakpoint in the constructor of the UserControl, I cant see a DataContext.

My MainView Ressources

 <Window.Resources>
    <Converter:DecreaseIntConverter x:Key="DecreaseIntConv" />
    <Converter:PortalSlotConverter x:Key="PortalSlotConverter" />

    <DataTemplate  x:Key="SXUserControl"
                   DataType="{x:Type ViewModel:MainViewModel}">
        <Resources:SXUserControl />
    </DataTemplate>
    <DataTemplate x:Key="XSUserControl"
                  DataType="{x:Type ViewModel:MainViewModel}">
        <Resources:XSUserControl/>
    </DataTemplate>
    <Helper:PropertyDataTemplateSelector x:Key="PropertyDataTemplateSelector"
                                         SXUserControl="{StaticResource SXUserControl}"
                                         XSUserControl="{StaticResource XSUserControl}" />
</Window.Resources>

The ItemsControl in my MainView

 <ItemsControl DockPanel.Dock="Right"
                              Width="Auto"
                              ItemsSource="{Binding MachineList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              ItemTemplateSelector="{StaticResource PropertyDataTemplateSelector}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid ShowGridLines="True"
                                  Helper:GridHelpers.RowCount="10"
                                  Helper:GridHelpers.ColumnCount="4"
                                  Helper:GridHelpers.StarColumns="0,1,2,3">
                            </Grid>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

My ItemTemplateSelector:

 public class PropertyDataTemplateSelector : DataTemplateSelector
{

    public DataTemplate SXUserControl { get; set; }
    public DataTemplate XSUserControl { get; set; }


    public override DataTemplate
    SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Machine)
        {
            Machine machineItem = item as Machine;

            if (machineItem.MachineType == MachineType.SX)
                return
                    SXUserControl;
            else
                return
                    XSUserControl;
                    //element.FindResource("XSUserControl") as DataTemplate;
        }
        return null;
    }

}

Now it works! Added to the constructor of UserControl

    public SXUserControl()
    {
        InitializeComponent();
        allowDropConverter = new AllowDropConverter();
        this.Loaded += new RoutedEventHandler(UserControl_Loaded); //!!!
    }

and the Event

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Window parentWindow = Window.GetWindow(this);
        mainViewModel = (MainViewModel)parentWindow.DataContext;
    }

Thx to Clemens for let me rethink to use Events!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM