Scrollbars not visible + Events not thrown - wpf

I would like to implement a userControl which is able to scroll and zoom with provided mouse input.
Therefore, I implemented following user Control. (If its working I am going to move the Events directly to the Model)
ScrollDragZoomControl.xaml
<UserControl x:Class="MinimalMonitoringClient.Controls.ScrollDragZoomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:models="clr-namespace:MinimalMonitoringClient.Controls.Models"
Background="Transparent">
<UserControl.DataContext>
<models:ScrollDragZoomViewModel />
</UserControl.DataContext>
<ScrollViewer x:Name="scrollViewer"
MouseLeftButtonUp="scrollViewer_MouseLeftButtonUp"
PreviewMouseLeftButtonUp="scrollViewer_PreviewMouseLeftButtonUp"
PreviewMouseWheel="scrollViewer_PreviewMouseWheel"
PreviewMouseLeftButtonDown="scrollViewer_PreviewMouseLeftButtonDown"
MouseMove="scrollViewer_MouseMove"
VerticalScrollBarVisibility="{Binding VerticalScrollBarVisibility}"
HorizontalScrollBarVisibility="{Binding HorizontalScrollBarVisibility}">
<Grid RenderTransformOrigin="0.5,0.5">
<Grid.LayoutTransform>
<TransformGroup>
<ScaleTransform />
</TransformGroup>
</Grid.LayoutTransform>
<Viewbox>
<!-- Present the actual stuff the user wants to display -->
<ContentPresenter />
</Viewbox>
</Grid>
</ScrollViewer>
In another UserControl I would like to use this UserControl so show Content which could be bigger than the current UserControl's width and height. For Testing I set Width and Height to something huge.
<UserControl x:Class="MinimalMonitoringClient.Panels.HierarchicalView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:panels="clr-namespace:MinimalMonitoringClient.Models.Panels"
xmlns:controls="clr-namespace:MinimalMonitoringClient.Controls">
<UserControl.DataContext>
<panels:HierarchicalViewModel />
</UserControl.DataContext>
<controls:ScrollDragZoomControl>
<Image Source="/Images/Information.png" Width="2000" Height="2000" IsHitTestVisible="False" />
</controls:ScrollDragZoomControl>
No Events are called in the ScrollDragZoomControl. I set the
Background to transparent. Also played with IsHitTestVisible.
The Scrollbars are not visible.

When you set the Content property of the UserControl to an Image element you effectively "override" any content that you have defined in ScrollDragZoomControl.xaml.
You probably want the ScrollViewer and the rest of the stuff you have defined in ScrollDragZoomControl.xaml to be part of the UserControl's template:
<UserControl x:Class="MinimalMonitoringClient.Controls.ScrollDragZoomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:models="clr-namespace:MinimalMonitoringClient.Controls.Models"
Background="Transparent">
<UserControl.DataContext>
<panels:HierarchicalViewModel />
</UserControl.DataContext>
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<ScrollViewer x:Name="scrollViewer"
MouseLeftButtonUp="scrollViewer_MouseLeftButtonUp"
PreviewMouseLeftButtonUp="scrollViewer_PreviewMouseLeftButtonUp"
PreviewMouseWheel="scrollViewer_PreviewMouseWheel"
PreviewMouseLeftButtonDown="scrollViewer_PreviewMouseLeftButtonDown"
MouseMove="scrollViewer_MouseMove"
VerticalScrollBarVisibility="{Binding VerticalScrollBarVisibility}"
HorizontalScrollBarVisibility="{Binding HorizontalScrollBarVisibility}">
<Grid RenderTransformOrigin="0.5,0.5">
<Grid.LayoutTransform>
<TransformGroup>
<ScaleTransform />
</TransformGroup>
</Grid.LayoutTransform>
<Viewbox>
<!-- Present the actual stuff the user wants to display -->
<ContentPresenter />
</Viewbox>
</Grid>
</ScrollViewer>
</ControlTemplate>
</UserControl.Template>
</UserControl>

Related

Viewbox is clipping my dockpanel

this is what my program looks like in my editor
this is a screen shot from the tablet my program is running on.
The xaml for said code is this
<Window x:Class="DLIUnitLibrary_WPF.ConfigureWindowLandscape"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DLIUnitLibrary_WPF"
xmlns:posButton="clr-namespace:DLIUnitLibrary_WPF.Buttons"
xmlns:UnitImagePanels="clr-namespace:DLIUnitLibrary_WPF.UnitImagePanels"
Background="{DynamicResource formBackground}"
Width="800"
Height="480"
WindowState="Maximized"
WindowStyle="None"
Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Viewbox Margin="10">
<DockPanel x:Name="mainView"
Height="480"
Width="800">
<local:DLIHeader DockPanel.Dock="Top"
Visibility="Hidden" />
<WrapPanel Width="125"
ItemHeight="125"
ItemWidth="125"
Margin="0,5,0,0"
DockPanel.Dock="Right"
Orientation="Vertical"
VerticalAlignment="Bottom">
<posButton:OposButton x:Name="msrButton"
Margin="5"
ImageSource="Images/msr_keymon.png" />
<posButton:OposButton x:Name="imagerButton"
Margin="5"
ImageSource="Images/barcode_keymon.png" />
<posButton:OposButton x:Name="brightButton"
Margin="5"
ImageSource="Images/brightness_keymon.png" />
</WrapPanel>
<Grid Margin="10">
<Viewbox x:Name="tablet9viewbox"
Visibility="Hidden">
<UnitImagePanels:Tablet9Image />
</Viewbox>
<Viewbox x:Name="tablet7viewbox" Visibility="Hidden">
<UnitImagePanels:Tablet7Image>
<UnitImagePanels:Tablet7Image.LayoutTransform>
<TransformGroup>
<ScaleTransform />
<SkewTransform />
<RotateTransform Angle="90" />
<TranslateTransform />
</TransformGroup>
</UnitImagePanels:Tablet7Image.LayoutTransform>
</UnitImagePanels:Tablet7Image>
</Viewbox>
</Grid>
</DockPanel>
</Viewbox>
</Window>
The screen resolution on the tablet7 is 800x480. The tablet has a emulated 800x600 and when I up it to that I can see all 3 buttons.
The screen resolution on the tablet9 is 1024x768 and does not have the 2 button problem infact it renders it perfectly. What am I missing?
EDIT
I forgot about my code behind, sorry about that. It doesn't too to much on the OnLoadedevent, but it is here
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.mainView.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.mainView.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
switch (DLIUnitFinder.GetDLIUnit())
{
case DLIUnit.tablet7:
this.tablet7viewbox.Visibility = System.Windows.Visibility.Visible;
break;
case DLIUnit.tablet9:
this.tablet9viewbox.Visibility = System.Windows.Visibility.Visible;
break;
}
}
For anyone else that is experiancing a similar problem it appears that Clipping is allowed with WrapPanel. I changed to the panel that has those 3 buttons in it from WrapPanel to UniformGrid and the buttons are now showing as they should.

WPF Window Not Showing Transparency Correctly

TI'm having some trouble getting my Window to be Transparent.
The Window is defined below:
<Window x:Class="HKC.Desktop.Views.UserInterfaces.RemoteKeypad"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Closing="Window_Close"
KeyDown="RemoteKeypad_KeyDown"
KeyUp="RemoteKeypad_KeyUp" MouseDown="OnMouseDown_Event"
Title="Title" Width="325" Height="370"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
I then have a border defined to give a curved edge to the Window, with a Grid inside for layout purposes:
<Border CornerRadius="20" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Border.Effect>
<DropShadowEffect BlurRadius="6" ShadowDepth="3" Color="#484948" />
</Border.Effect>
<Grid Background="Transparent" VerticalAlignment="Stretch">
....
</Grid>
</Border>
</Window>
For some reason, the first time that I open the window the background is not transparent
However, If I click away from the Window, back back on it, the transparency is working as expected.
Turns out the problem was with an external tool for adding Drop Shadows to elements.

Transfer Content value of a UserControl to a Control inside it

I need to extend the bing maps control to be more user MVVM friendly (in specific the ZoomLevel and the BoundingRect properties are not Dependency Properties). I am wrapping the control in a custom usercontrol (I also need to add elements to make other map choices e.g. google maps). I need to transfer the Content Value of the UserControl to the BingMapsControl :
<UserControl x:Class="RevOptWebControls.MVVMMapControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
xmlns:mCore="clr-namespace:Microsoft.Maps.MapControl.Core;assembly=Microsoft.Maps.MapControl"
d:DesignHeight="300" d:DesignWidth="400"
x:Name="Root">
<Grid x:Name="LayoutRoot" Background="White">
<m:Map CredentialsProvider="Al_H1LepP6chseYMu31RK76El6k4SUkx2KVrxeqobE3rTXooFPieuEJ6qiuA211I"
CopyrightVisibility="Collapsed"
LogoVisibility="Collapsed"
ScaleVisibility="Visible"
NavigationVisibility="Visible"
x:Name="MyMap">
</m:Map>
<ComboBox x:Name="c_MapTypes" HorizontalAlignment="Right" VerticalAlignment="Top" SelectedIndex="0" Height="30" SelectionChanged="MapTypes_SelectionChanged">
<ComboBoxItem>Google Roads</ComboBoxItem>
<ComboBoxItem>Google Aerial</ComboBoxItem>
<ComboBoxItem>Bing Maps Roads</ComboBoxItem>
<ComboBoxItem>Bing Maps Aerial</ComboBoxItem>
<ComboBoxItem>Open Street Maps</ComboBoxItem>
<ComboBoxItem>Yahoo Street</ComboBoxItem>
<ComboBoxItem>Yahoo Aerial</ComboBoxItem>
<ComboBoxItem>Blank Map</ComboBoxItem>
</ComboBox>
</Grid>
</UserControl>
Update : Figured out how to do it. Shared the control source code as well : http://basaratali.blogspot.com/2010/12/mvvm-version-of-bing-maps-with-google.html
Why not try a Custom Control with custom template. Use
`{TemplateBinding Content}`
to bind Content with Map control.
Example:
<Style TargetType="local:MVVMMapControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MVVMMapControl">
<Grid x:Name="LayoutRoot"
Background="White">
<m:Map CredentialsProvider="Al_H1LepP6chseYMu31RK76El6k4SUkx2KVrxeqobE3rTXooFPieuEJ6qiuA211I"
CopyrightVisibility="Collapsed"
LogoVisibility="Collapsed"
ScaleVisibility="Visible"
NavigationVisibility="Visible"
x:Name="MyMap"
Content="{TemplateBinding ContentControl.Content}"></m:Map>
<ComboBox x:Name="c_MapTypes"
HorizontalAlignment="Right"
VerticalAlignment="Top"
SelectedIndex="0"
Height="30"
SelectionChanged="MapTypes_SelectionChanged">
<ComboBoxItem>Google Roads</ComboBoxItem>
<ComboBoxItem>Google Aerial</ComboBoxItem>
<ComboBoxItem>Bing Maps Roads</ComboBoxItem>
<ComboBoxItem>Bing Maps Aerial</ComboBoxItem>
<ComboBoxItem>Open Street Maps</ComboBoxItem>
<ComboBoxItem>Yahoo Street</ComboBoxItem>
<ComboBoxItem>Yahoo Aerial</ComboBoxItem>
<ComboBoxItem>Blank Map</ComboBoxItem>
</ComboBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Class:
public class MVVMMapControl : ContentControl // Notice this inherits from ContentControl for its Content Property
{
public MVVMMapControl()
{
this.DefaultStyleKey = typeof(MVVMMapControl);
}
}

Keeping Canvas overflow in WPF

I have a (hopefully) interesting question.
First of all what I'm trying to do here:
I'm trying to create a pie-chart-like set of buttons, later this control will be used within a touch enabled application. The control draws and looks just fine, also all of the behaviours are fine so far. However, one thing that I'm having issues with is the translations I do to all the pieces of the pie.
SO what I do is: I want margin n between the pie pieces, to create this margine I move all pieces away from the middle. This means that the pie piece that's facing UP will have a negative translation. This in turn means that the Canvas will clip a part of the top (due the top being at 14, -2 for example). Another thing that I've added are pieces of text which are also making the pie pieces quite a bit longer. See the image included for reference.
image
To the left you can see the clipping issue I'm talking about to the right you can see an arbitrarily translated version of the same thing.
Some code paste:
Main window XAML:
<controls:PieMenu Radius="100" Padding="10">
<controls:PieMenu.MenuItems>
<controls:PieMenuItem Text="Employment" Brush="#FF33FF" Command="BrowseBack" />
<controls:PieMenuItem Text="General" Brush="#9933FF" Command="BrowseBack" />
<controls:PieMenuItem Text="Internships" Brush="#3333FF" Command="BrowseBack" />
<controls:PieMenuItem Text="Bla" Brush="#3399FF" Command="BrowseBack" />
<controls:PieMenuItem Text="Bla" Brush="#007AF5" Command="BrowseBack" />
</controls:PieMenu.MenuItems>
PieMenu XAML:
<UserControl x:Class="PieControlLibrary.PieMenu"
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:self="clr-namespace:PieControlLibrary"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<!-- <CollectionViewSource x:Name="menuItemCollectionViewSource" Source="{Binding MenuItems, RelativeSource={RelativeSource AncestorType=self:PieMenu}}"/>-->
<Style TargetType="{x:Type ListView}" x:Key="listViewStyle">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<self:PieButton Radius="{Binding Radius, RelativeSource={RelativeSource AncestorType=self:PieMenu}}"
Degrees="{Binding Degrees, RelativeSource={RelativeSource AncestorType=self:PieMenu}}"
Brush="{Binding Brush}"
Command="{Binding Command}"
Text="{Binding Text}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<ListView x:Name="menuItemsView" ItemsSource="{Binding MenuItems, RelativeSource={RelativeSource AncestorType=self:PieMenu}}" Style="{StaticResource listViewStyle}" />
</Grid>
PieButton (this is what the pie menu items are converted to)
<UserControl x:Class="PieControlLibrary.PieButton"
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:control="clr-namespace:PieControlLibrary"
xmlns:TextOnPath="clr-namespace:Petzold.TextOnPath"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
>
<Grid>
<Button HorizontalAlignment="Left" VerticalAlignment="Top" DataContext="{Binding RelativeSource={RelativeSource AncestorType=control:PieButton}}" Command="{Binding Command}">
<Button.Template>
<ControlTemplate>
<Canvas >
<Path Data="{Binding PathData}" Fill="{Binding Brush}" Grid.Row="0" Grid.Column="0" />
<TextOnPath:TextOnPathControl PathFigure="{Binding TextPath}" Text="{Binding Text}" FontFamily="Consolas" FontSize="12" Foreground="Black" FontStretch="Normal" />
</Canvas>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
I'm not sure if I understand your question, but when you draw your pie chart, just leave some extra space around the edges so you can move the pie pieces out from the center?
Perhaps you could use a RenderTransform when your pie pieces expand to shift or scale the image a tiny bit to keep it in bounds?

combine image and solidcolor background for a WPF Form

I have a WPF form that I am building. I want to specify a background image for the window, which is easy enough. However, I also want to specify a color so that the area of the form not covered by the image is white. I've seen some examples that show using two different background brushes, but when I try that VS.NET tells me I can't have multiple brushes.
This is the XAML I'm using
<Window x:Class="Consent.Client.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/CompositeWPF"
Title="Shell" WindowStyle="None" WindowState="Maximized" FontSize="24">
<Window.Background>
<ImageBrush AlignmentX="Left" AlignmentY="Top" Stretch="None" TileMode="None" ImageSource="logo_header2.png" />
</Window.Background>
<ItemsControl Background="White" VerticalAlignment="Center" cal:RegionManager.RegionName="MainRegion" >
</ItemsControl>
</Window>
This works great for the image, but the background not covered by the image is black. How do I make it white? Changing the image itself is not really an option.
Try this (I removed everything not directly relevant to the question to make the code clearer):
<Window x:Class="Consent.Client.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="White">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="logo_header2.png" />
</Grid.Background>
<ItemsControl>
</ItemsControl>
</Grid>
</Window>
Basically, set the window's background to the behind the image color, than put a grid in the window and give the grid you background image, put everything inside the grid instead of directly in the window.
As an Extension to Nirs answer. If you want to have margins around your content, but let the background image be able to fill the whole window, you can also stack backgrounds using borders:
<Window x:Class="Consent.Client.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="White">
<Border Padding="10">
<Border.Background>
<ImageBrush ImageSource="logo_header2.png" />
</Border.Background>
<!--<Your content >-->
</Border>
</Window>
I'm not sure you can combine brushes. You could play around with ImageBrush, or you could forget the "background" and stack the items on top of each other in a Grid:
<Window x:Class="Consent.Client.Shell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.codeplex.com/CompositeWPF"
Title="Shell" WindowStyle="None" WindowState="Maximized" FontSize="24">
<Grid>
<Image Source="logo_header2.png" Stretch="None" VerticalAlignment="Top" />
<ItemsControl Background="White" VerticalAlignment="Center" cal:RegionManager.RegionName="MainRegion" >
</ItemsControl>
</Grid>
</Window>

Resources