Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Datacontext for included user control

$
0
0

Hello,

I have a XAML window named MainPresentation in which I include a User Control named CircularGraph. MainPresentation has a resource (for DataContext) named MainPresentationVM (see below). I want this resource also to be available inside the included user control. I am wondering if I am doing it correctly.

Below are relevant code in MainPresentation:

<Window x:Class="Visningsprogram.View.MainPresentation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Visningsprogram.ViewModel"
        xmlns:localv="clr-namespace:Visningsprogram.View"
        Title="MainPresentation" Height="600" Width="800"><Window.Resources><local:MainPresentationVM x:Key="MainPresentationVM"/></Window.Resources><Grid DataContext="{StaticResource MainPresentationVM}"><TabControl Height="561" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="778"><TabItem Header="Visualize" Name="visualizeTab"><localv:CircularGraph/></TabItem>
            ...

Then inside the included user control CircularGraph.xaml:

<UserControl x:Class="Visningsprogram.View.CircularGraph" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Visningsprogram.ViewModel" Height="330" Width="330"><UserControl.Resources><local:MainPresentationVM x:Key="MainPresentationVM"/></UserControl.Resources> ...

I guess this should be enough to have this resource available in the user control?

In addition I have a

<Canvas Height="330" Width="330" DataContext="{StaticResource MainPresentationVM}">

inside this user control, as well as the binding to an Angle property inside this Canvas:

<Line.RenderTransform><RotateTransform x:Name="RoteringSanntid" CenterX="165" CenterY="145" Angle="{Binding WindDirection}" /></Line.RenderTransform>

WindDirection is a property in class MainPresentationVM.

I am running another thread reading from the serial port, and it seems that it somehow gets problems reading data from the serial port when I include the DataContext attribute on the Canvas as seen above. I don't know how this is related. Perhaps I do this incorrectly and the other thread get problems?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>