Content overriding entire user control, rather than ContentPresenter - c#

I'm guessing this is probably an easy mistake I am making somewhere. I have a custom control, stripped down to the basics:
<local:CustomUserControl x:Class="Test.UI.CustomUserControl"
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:local="clr-namespace:Test.UI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
IsEnabled="True" Loaded="CustomUserControl_Loaded">
<!-- Main border -->
<Border BorderBrush="#9B000000" BorderThickness="1" Margin="0,0,0,0" Padding="0">
<Grid Margin="0" Name="outerGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Margin="0" Grid.Row="0" Grid.Column="0" Name="innerGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- Backgound -->
<Rectangle Fill="#FF5B87B8" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="headerRectangle" PreviewMouseLeftButtonUp ="headerRectangle_MouseLeftButtonUp" />
<!-- Text -->
<Label Grid.Row="0" Grid.Column="0" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Name="HeaderLabel" Content="Hello" Margin="5,0,0,0" Foreground="White" FontSize="18" Background="#FF5B87B8" PreviewMouseLeftButtonUp ="HeaderLabel_MouseLeftButtonUp" />
</Grid>
<!-- Content -->
<ContentPresenter Name="MainContent" Grid.Row="1" Grid.Column="0" Content="{Binding Content}" />
</Grid>
</Border>
</local:CustomUserControl>
When displaying on the form, like the following, it draws a box, with a shaded top, with white text:
<ui:CustomUserControl Grid.Row="0" Grid.Column="2" Name="borderDiagram" Header="Hello" />
But, if I try and add content to the ContentPresenter:
<ui:CustomUserControl Grid.Row="0" Grid.Column="2" Name="borderDiagram" Header="Hello">
<Label Content="Um..." />
</ui:CustomUserControl >
It overrides the entire custom control, leaving only the 'Um...' label.
I presume I am managing to override the entire control when I set the content, so how does one ensure that it's the ContentPresenter that takes the content, rather than the parent control?

CustomUserControl has some default Content:
<!-- Main border -->
<Border> ...
</Border>
and that Content is replaced with <Label Content="Um..." />
to make it work as expected (Label displayed in ContentPresenter) you should define default template:
<UserControl.Template>
<ControlTemplate TargetType="UserControl">
<Border BorderBrush="#9B000000" BorderThickness="1" Margin="0,0,0,0" Padding="0">
<Grid Margin="0" Name="outerGrid">
...
<!-- Content -->
<ContentPresenter Name="MainContent" Grid.Row="1" Grid.Column="0"
Content="{TemplateBinding Content}" />
</Grid>
</Border>
</ControlTemplate>
</UserControl.Template>
pay attention to one important change:
Content="{TemplateBinding Content}"
ContentPresenter uses template binding to get and display custom content

Related

Why does the border stop rendering its minus margin while resizing the grid

I've used a border with negativ margin to mark a grid row. But I get a strange behaviour while resizing the window. Cutting the second column of the row makes the margin of the border disappear:
Of course this is a small example, in the main application I'm using a grid splitter, but the bahaviour stays the same. Is it possible to fix this somehow or is it a WPF bug?
MainWindow.xaml:
<Window x:Class="TestHighlightBorder.MainWindow"
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"
xmlns:local="clr-namespace:TestHighlightBorder"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="100,0,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Border Background="DarkRed" Opacity="0.3" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="-80,0,0,0" Panel.ZIndex="1" />
<TextBlock Text="column 0" Background="LightBlue" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="column 2" Background="LightGreen" Grid.Row="0" Grid.Column="2" />
</Grid>
ps:
This is how the main application looks like. It's some sort of a propertygrid. The amount of the negativ margin depends on the level of nested objects.
I tried your example and it happens just as you said: column 2 gone, margin gone.
This seems to happen whenever the grid can't be displayed completely.
If, for example you set the third column definition to 200, the margin disappears as soon as column 2 isn't shown in it's entirety. Same thing happens when you resize the window from the bottom.
If you put the existing grid in another container (Grid, StackPanel, etc.) and set the MinWidth to something at least the size of the width of the columns + margin (in your example 310), this doesn't happen.
Like so:
<StackPanel MinWidth="310">
<Grid Margin="100,0,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Border Background="DarkRed" Opacity="0.3" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="-80,0,0,0" Panel.ZIndex="1" />
<TextBlock Text="column 0" Background="LightBlue" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="column 2" Background="LightGreen" Grid.Row="0" Grid.Column="2" />
</Grid>
</StackPanel>
Instead of adding a 100 left-margin to the grid, you could just fix a column at the beginning with the width of 100, set the column-span of the border to 4, replace the negative margin with 20 positive left-margin (100-80=20), and add 1 to the value of Grid.Column for each of your controls. So the final approach would look like that:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Border Background="DarkRed" Opacity="0.3" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="20,0,0,0" Panel.ZIndex="1" />
<TextBlock Text="column 0" Background="LightBlue" Grid.Row="0" Grid.Column="1" />
<TextBlock Text="column 2" Background="LightGreen" Grid.Row="0" Grid.Column="3" />
</Grid>

Why UserControl isn't filling VerticalContentAlignment 'Stretch'?

Here is the parent Xaml:
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="*" /><!-- Should stretch vertically -->
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<!-- Menu definition removed for brevity -->
</DockPanel>
<DockPanel Grid.Row="1"
VerticalAlignment="Stretch"
LastChildFill="True">
<ItemsControl DockPanel.Dock="Top"
ItemsSource="{Binding Designer}"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Background="YellowGreen"/>
</DockPanel>
</Grid>
The ItemsSource binding is to an ObservableCollection. When the view is added to the collection, it gets updated in the main shell (view). Here is the UserControl that is added:
<UserControl x:Class="Prototype.StateMachineDesignerApp.Views.ProjectDesigner"
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:local="clr-namespace:Prototype.StateMachineDesignerApp.Views"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="500"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Margin="0">
<Grid VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Border Grid.Row="0"
BorderBrush="DimGray"
BorderThickness="1"
Background="LightSlateGray"
Margin="1" />
<Border Grid.Row="1"
Margin="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="3" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
BorderBrush="DarkGoldenrod"
BorderThickness="1" />
<GridSplitter Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
<Border Grid.Column="2"
BorderBrush="DarkGoldenrod"
BorderThickness="1" />
</Grid>
</Border>
</Grid>
</UserControl>
The UserControl is not filling the entire vertical space:
Row[1] of the outermost Grid is stretching vertically as is evidenced by the ItemsControl.Background filling the area.
For reference, this is what I am expecting:
For what its worth, I've looked at numerous questions on SO regarding this exact issue but none of the solutions seem to help. Then again, none of the examples I saw used an ItemsControl with binding.
This happens because the ItemsControl throw all of our items into a vertically aligned StackPanel by default. It's very easy to change though, since the ItemsControl allows you to change which panel type is used to hold all the items.
<ItemsControl DockPanel.Dock="Top"
ItemsSource="{Binding Designer}"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Background="Transparent">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>

WPF Grid Rows and ResizeGrip do not anchor correctly

once again after hours of struggle with WPF I need your awesome help.
I have read many different StackOverflows, nonetheless, I cannot seem to get my current situation working and I don't understand why. I have chosen to use a Grid in contrast to a DockPanel and I would like to keep it that way as well. According to what I have read the row definitions are ok, the second row of the template grid should automatically stretch. Unfortunately it does not, as it seems that the last row of the grid does not move away.
This is how my window looks at the moment:
As you can see is the blue row not at the bottom of the window. In fact, the window should not be that long in the first place.
This is the code snippet of my Generic.xaml file that is relevant to the problem.
The way it works is, that I have a Skin with a custom control group:
<!-- Window START-->
<ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
<Border BorderBrush="LightGray" BorderThickness="1" Background="Red" Padding="0" VerticalAlignment="Stretch">
<Grid x:Name="WindowRoot" Margin="0" VerticalAlignment="Top" Background="Blue" MouseDown="Window_MouseDown">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="*" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<!-- HEADER START-->
<Frame x:Name="header_background" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="0" Background="#ddd" BorderThickness="0 0 0 1" BorderBrush="#c9c9c9"/>
<Image x:Name="LogoICon" Source="/MTApp;component/Resources/Icon.png" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" Margin="10 8"/>
<Label x:Name="windowTitle" Grid.ColumnSpan="2" Content="{TemplateBinding Title}" VerticalAlignment="Center" Foreground="#393939" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" FontFamily="Segoe UI Regular" FontSize="12"/>
<Grid Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="18"/>
<ColumnDefinition Width="19"/>
<ColumnDefinition Width="18"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="20" />
<RowDefinition Height="10" />
</Grid.RowDefinitions>
<Button x:Name="minimizeBtn" Content="0" FontFamily="Marlett" Foreground="#393939" Background="Transparent" BorderThickness="0" Grid.Row="1" Grid.Column="1" Margin="3 0 0 0" Click="minimizeBtn_Click"/>
<Button x:Name="maximizeBtn" Content="2" FontFamily="Marlett" Foreground="#393939" Background="Transparent" BorderThickness="0" Grid.Row="1" Grid.Column="2" Margin="3 0 0 0" Click="maximizeBtn_Click"/>
<Button x:Name="quitBtn" Content="r" FontFamily="Marlett" Foreground="#393939" Background="Transparent" BorderThickness="0" Grid.Row="1" Grid.Column="3" Margin="3 0 0 0" Click="quitBtn_Click"/>
</Grid>
<!-- HEADER END-->
<!-- CONTENT START-->
<ContentPresenter Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1" Height="Auto"/>
<!-- CONTENT END-->
<!-- FOOTER START-->
<Border x:Name="footer_background" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="2" Height="25">
<ResizeGrip />
</Border>
<!-- FOOTER END-->
</Grid>
</Border>
</ControlTemplate>
<!-- Window END-->
<Style x:Key="LightSkin_0_1" TargetType="Window">
<Setter Property="WindowStyle" Value="None" />
<Setter Property="AllowsTransparency" Value="True" />
<Setter Property="ResizeMode" Value="CanResizeWithGrip" />
<Setter Property="Template" Value="{StaticResource WindowTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding WindowState}" Value="Maximized">
</DataTrigger>
</Style.Triggers>
</Style>
And my MainWindow.xaml is:
<Window x:Class="UHashIt.MainWindow"
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"
xmlns:local="clr-namespace:UHashIt"
mc:Ignorable="d"
Title="Window title"
WindowStartupLocation="CenterScreen"
MinHeight="275"
MinWidth="700"
Width="700"
Style="{DynamicResource LightSkin_0_1}">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- Menu -->
<Border VerticalAlignment="Top" Padding="5" Background="#f2f2f2" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" BorderThickness="0 0 0 1" BorderBrush="LightGray">
<DockPanel >
<Menu DockPanel.Dock="Left">
<MenuItem Header="File">
<MenuItem Header="Add File" />
<MenuItem Header="Export.." />
<Separator />
<MenuItem Header="Close"/>
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="Online Documentation" />
<MenuItem Header="About"/>
</MenuItem>
</Menu>
</DockPanel>
</Border>
<!-- Menu End -->
<!-- Content -->
<Grid Grid.Row="1" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="150*"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="150*"/>
<ColumnDefinition Width="25"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="40" />
<RowDefinition Height="30" />
<RowDefinition Height="40" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Label Style="{DynamicResource headline4}" Content="File 1" Grid.Row="0" Grid.Column="1"/>
<TextBox Grid.Row="1" Style="{DynamicResource NormalTextBox}" Grid.Column="1"/>
<Button Grid.Column="2" Grid.Row="1" Style="{DynamicResource CommonButton}" Height="30" Width="30" VerticalAlignment="Top"/>
<Label Style="{DynamicResource headline5}" Grid.Row="1" Grid.Column="3"/>
<Label Style="{DynamicResource headline4}" Content="File 2" Grid.Row="2" Grid.Column="1"/>
<TextBox Grid.Row="3" Style="{DynamicResource NormalTextBox}" Grid.Column="1"/>
<Button Grid.Column="2" Grid.Row="3" Style="{DynamicResource CommonButton}" Height="30" Width="30" VerticalAlignment="Top"/>
<Label Style="{DynamicResource headline5}" Grid.Row="3" Grid.Column="3"/>
<!-- Hash Button and Selection -->
<ComboBox Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" Width="200" Height="30">
<ComboBoxItem>
Please Choose Your Algorithm
</ComboBoxItem>
</ComboBox>
<Button Grid.Column="1" Grid.Row="4" Style="{DynamicResource ActionBtn}" Content="Button" HorizontalAlignment="Right"/>
<!-- End-->
</Grid>
<!-- Content End-->
</Grid>
I cannot explain myself why the "blue bar" is not attached to the bottom. Despite the fact, that the window is so big.
I think you want to change this:
<Grid x:Name="WindowRoot" Margin="0" VerticalAlignment="Top" Background="Blue" MouseDown="Window_MouseDown">
to this:
<Grid x:Name="WindowRoot" Margin="0" VerticalAlignment="Stretch" Background="Blue" MouseDown="Window_MouseDown">

Scale while keep horizontal align

I have WPF control:
<UserControl x:Class="MyProject.LabelWithUnit"
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"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="150">
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid Width="150">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
<Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" Margin="1" FontSize="10" />
</Grid>
</Viewbox>
</UserControl>
How it behave now:
Default:
When width increased:
(this is what i need to change)
That how it scales when i increase height:(that should stay as it is)
How it should behave:
(only this behavior should change - "m/s" should stick to top-right corner):
So, "m/s" part should always stick to top-right corner and numeric part should stay somehow near middle. When i increase Height of my control, it should scale both Labels.
EDIT: more pictures added.
Change the stretching on the ViewBox control:
<Viewbox StretchDirection="UpOnly" Stretch="Uniform">
have you tried
<Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" FontSize="10" HorizontalAlignment="Right" VerticalAlignment="Top" />
EDIT: I am also unsure how these columns are helping you
I get the style you wish simply swapping VerticalAlignment of Labels to Stretch
<UserControl x:Class="MyProject.LabelWithUnit"
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"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="150">
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid Width="1900">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Stretch" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
<Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" Margin="1" FontSize="10" VerticalAlignment="Stretch"/>
</Grid>
</Viewbox>
</UserControl>
Try removing Width from your grid will solve your problem.
<Grid Width="150">
Edit
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="25" />
</Grid.ColumnDefinitions>
<Label x:Name="ValueLabel" Grid.Column="0" Content="1013.0" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
<Label x:Name="UnitLabel" Grid.Column="1" Content="m/s" FontSize="10" />
</Grid>
EDIT
As try this below,
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0" MaxHeight="150" MinHeight="10" MaxWidth="800" MinWidth="10" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Label x:Name="ValueLabel" Content="1013.0" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="18"/>
</Viewbox>
<Viewbox Grid.Column="1" MaxHeight="150" MinHeight="10" MaxWidth="800" MinWidth="10" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Label x:Name="UnitLabel" Content="m/s" FontSize="10" />
</Viewbox>
</Grid>
<Grid>
<Viewbox Margin="0">
<Label Margin="0" Padding="1" FontWeight="Bold"/>
</Viewbox>
<Grid SnapsToDevicePixels="True">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="0.8*" />
</Grid.RowDefinitions>
<Viewbox HorizontalAlignment="Right" Stretch="Uniform" Margin="3,0">
<Label Margin="0" Padding="0"/>
</Viewbox>
</Grid>
</Grid>

WPF window shows blank

Excel AddIn, c#, .net 4.0, windows 7, ribbon
my addin has a ribbon tab, several ribbon buttons,
when one ribbon is clicked, the addin will send several web service calls and a window will pop up providing data in tab, treeview, gridview etc.
data in treeview, gridview are populated from web service calls.
All worked for a particular end user until yesterday
When he clicked button, the window seems showing up, but it is kind of behind Excel and could not be focused. Also it is blank with no tab, treeview, gridview, etc.
I verified all web service calls return properly.
The user has windows 7, Excel 2010 (32 bit).
I have no idea what could cause this. Please help.
this is my WPF window. thanks
<Window x:Class="MIMICWPFLib.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sharedC="clr-namespace:MIMICShared.Converter;assembly=MstarCommodityShared"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxt="clr-namespace:DevExpress.Xpf.Utils.Themes;assembly=DevExpress.Xpf.Core.v11.2"
xmlns:Controls="clr-namespace:MIMICWPFLib.Controls"
Title="{Binding Title}"
Height="600" Width="850" Top="223" Left="164" ResizeMode="CanResize" Closing="WindowClosing"
WindowStyle="ToolWindow">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainWindowResources.xaml" />
<ResourceDictionary Source="Controls/BizzySpinner.xaml" />
</ResourceDictionary.MergedDictionaries>
<sharedC:BooleanToHiddenVisibility x:Key="boolToVis"/>
<sharedC:NegativeBooleanToHiddenVisibiltyConverter x:Key="negativeBoolToVis" />
<DataTemplate x:Key="{dxt:DXTabControlThemeKey ResourceKey=BackgroundTemplate, ThemeName=Office2007Silver}">
<Border BorderBrush="#FF828790" BorderThickness="1" Background="#E5E3E3"/>
</DataTemplate>
<DataTemplate x:Key="{dxt:DXTabControlThemeKey ResourceKey=BackgroundTemplate}">
<Border BorderBrush="#FF828790" BorderThickness="1" Background="White"/>
</DataTemplate>
<ControlTemplate x:Key="{dxt:DXTabControlThemeKey ResourceKey=TopLayoutTemplate}" TargetType="{x:Type dx:DXTabControl}">
<Grid>
<KeyboardNavigation.TabNavigation>Local</KeyboardNavigation.TabNavigation>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="0,2,0,0" x:Name="tabHeadersPanel">
<KeyboardNavigation.TabIndex>1</KeyboardNavigation.TabIndex>
<KeyboardNavigation.DirectionalNavigation>Cycle</KeyboardNavigation.DirectionalNavigation>
<KeyboardNavigation.TabNavigation>Once</KeyboardNavigation.TabNavigation>
<Panel.ZIndex>1</Panel.ZIndex>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<dx:ClippedContainer Grid.Column="0" UseLayoutRounding="{TemplateBinding UseLayoutRounding}"
Style="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=ClippedContainerTopLayoutStyle}}">
<dx:TabPanelContainer x:Name="panelContainer" Style="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=PanelContainerTopLayoutStyle}}">
<dx:TabPanelContainer.Resources>
<Storyboard x:Key="ScrollStoryboard">
<DoubleAnimation Storyboard.TargetName="ItemsPanelTranslate"
Storyboard.TargetProperty="X" Duration="0:0:0.4" To="0">
<DoubleAnimation.EasingFunction>
<ExponentialEase EasingMode="EaseOut" Exponent="0" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</dx:TabPanelContainer.Resources>
<ItemsPresenter>
<ItemsPresenter.RenderTransform>
<TranslateTransform x:Name="ItemsPanelTranslate" />
</ItemsPresenter.RenderTransform>
</ItemsPresenter>
</dx:TabPanelContainer>
</dx:ClippedContainer>
<dx:TabControlScrollButton x:Name="PrevButton" Grid.Column="1" Style="{DynamicResource {dxt:DXTabControlThemeKey ResourceKey=PrevButtonStyle}}"
Margin="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=ComponentsSpaceForHorizontalLayouts}}" />
<dx:TabControlScrollButton x:Name="NextButton" Grid.Column="2" Style="{DynamicResource {dxt:DXTabControlThemeKey ResourceKey=NextButtonStyle}}"
Margin="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=ComponentsSpaceForHorizontalLayouts}}" />
<!--<dx:HeaderMenu Grid.Column="3" x:Name="HeaderMenu" IsTabStop="False" Style="{DynamicResource {dxt:DXTabControlThemeKey ResourceKey=HeaderMenuStyle}}"
Margin="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=ComponentsSpaceForHorizontalLayouts}}" />-->
<Controls:HeaderMenuForDXTabControl Grid.Column="3"
x:Name="HeaderMenu"
IsTabStop="False"
Style="{DynamicResource {dxt:DXTabControlThemeKey ResourceKey=HeaderMenuStyle}}"
Margin="{DynamicResource {dxt:DXTabControlInternalThemeKey ResourceKey=ComponentsSpaceForHorizontalLayouts}}" />
</Grid>
<Grid Grid.Row="1">
<dx:DXContentPresenter ContentTemplate="{DynamicResource {dxt:DXTabControlThemeKey ResourceKey=BackgroundTemplate}}" IsTabStop="False">
</dx:DXContentPresenter>
<Grid Margin="1">
<dx:DXContentPresenter x:Name="contentPresenter" Margin="{TemplateBinding Padding}" UseLayoutRounding="{TemplateBinding UseLayoutRounding}"
Content="{TemplateBinding SelectedItemContent}" ContentTemplate="{TemplateBinding SelectedItemContentTemplate}">
<KeyboardNavigation.TabNavigation>Local</KeyboardNavigation.TabNavigation>
<KeyboardNavigation.DirectionalNavigation>Contained</KeyboardNavigation.DirectionalNavigation>
<KeyboardNavigation.TabIndex>2</KeyboardNavigation.TabIndex>
</dx:DXContentPresenter>
<dx:TabControlFastRenderPanel x:Name="fastRenderPanel" Margin="{TemplateBinding Padding}" UseLayoutRounding="{TemplateBinding UseLayoutRounding}" Visibility="Collapsed">
<KeyboardNavigation.TabNavigation>Local</KeyboardNavigation.TabNavigation>
<KeyboardNavigation.DirectionalNavigation>Contained</KeyboardNavigation.DirectionalNavigation>
<KeyboardNavigation.TabIndex>2</KeyboardNavigation.TabIndex>
</dx:TabControlFastRenderPanel>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid Visibility="{Binding ShowProgress, Converter={StaticResource negativeBoolToVis}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="65"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto" MinWidth="2"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row ="0" Height="37" Width="174" Margin="11,6,0,0"
Name="image1" Stretch="Fill" VerticalAlignment="Top"
HorizontalAlignment="Left"
Source="/MstarCommodityWPFLib;component/Resources/MorningstarLogo_Red.gif" />
<Border Grid.ColumnSpan="1" Grid.Row="0" HorizontalAlignment="Right" Margin="0,10,0,0"
Height="50" Width="400" Background="white" BorderThickness="1" BorderBrush="White" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="23"/>
<RowDefinition Height="4"/>
<RowDefinition Height="23"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentControl Content="{Binding Path=SearchBox}" Margin="0 0 5 0" />
<TextBlock Grid.Row="0" Grid.Column="1">
<Hyperlink Click="ShowSettings" TextDecorations="None">
<Image Source="{Binding ConfigImageFilePath}" ></Image>
</Hyperlink>
</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1">
<Hyperlink Click="HelpHyperlinkClick">
<Image Source="{Binding HelpIconFilePath}"></Image>
</Hyperlink>
</TextBlock>
</Grid>
</Border>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Name="columnWidth" MaxWidth="350"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid x:Name="horizontalGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="125"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Name="rowHeight" MinHeight="150"></RowDefinition>
</Grid.RowDefinitions>
<dx:DXTabControl Grid.Row="0"
Margin="10 0 5 5"
Name="MainTabRegion"
SelectedIndex="{Binding Tabs.SelectedIndex}"
ItemsSource="{Binding Tabs.TabItems}"
DestroyContentOnTabSwitching="False" BorderThickness="5"
OverridesDefaultStyle="True">
<dx:DXTabControl.View>
<dx:TabControlScrollView
ShowHeaderMenu="True"
AllowHideTabItems="True"
CloseHeaderMenuOnItemSelecting="True" ShowHiddenTabItemsInHeaderMenu="True" />
</dx:DXTabControl.View>
<dx:DXTabControl.ItemContainerStyle>
<Style TargetType="{x:Type dx:DXTabItem}">
<Setter Property="Visibility" Value="{Binding IsVisible, Mode=OneWay, Converter={StaticResource boolToVis}}"/>
</Style>
</dx:DXTabControl.ItemContainerStyle>
<dx:DXTabControl.ItemHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header}"
Visibility="{Binding IsVisible, Mode=OneWay, Converter={StaticResource boolToVis}}"
OverridesDefaultStyle="True" />
</DataTemplate>
</dx:DXTabControl.ItemHeaderTemplate>
</dx:DXTabControl>
<GridSplitter Grid.Row="1"
Margin="10 0 5 5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ShowsPreview="true"
ResizeDirection="Rows"
Height="5" />
<Border Grid.Row="2" Margin="10 0 5 5">
<ContentControl Content="{Binding Path=Basket}" />
</Border>
</Grid>
<GridSplitter Margin="0 20 0 0" Grid.Column="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ShowsPreview="true"
ResizeDirection="Columns"
x:Name="verticalSplitter"
DragCompleted="OnDragCompleted"
Width="5" />
<Border Grid.RowSpan="3" Grid.Column="2" Margin="3 20 0 0">
<ContentControl Content="{Binding Path=ColumnDataPreview}" />
</Border>
</Grid>
</Grid>
<Grid Height="25" Width="300" Visibility="{Binding ShowProgress, Converter={StaticResource boolToVis}}">
<ProgressBar IsIndeterminate="True" Orientation="Horizontal" />
<Viewbox>
<TextBlock Text="Loading ..." Padding="50 0"/>
</Viewbox>
</Grid>
</Grid>
I know this is old -- but we stumbled on to this with a legacy Office add-in that we support -- where one user is apparently experiencing this same issue.
We did some digging, and stumbled across this similar SO post: Blank WPF child windows on Windows 10
It looks like there are two options:
Instruct the user to update/fix their bad video card drivers.
Put a hack into the code that will force the content to redraw at just the right instant (i.e. InvalidateVisual is not sufficient, see the linked question's answers for more information).

Categories

Resources