I'm Trying to get the value of a slider thats contained in a window from a usercontrol thats also contained in that window.
this is what i would like to accomplish.
<Window x:Class="TestApp3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Window.Resources>
<Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
<Setter Property="Value" Value="10" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Interval" Value="1" />
<Setter Property="Minimum" Value="5" />
<Setter Property="Maximum" Value="50" />
<Setter Property="TickFrequency" Value="0.25" />
<Setter Property="IsSnapToTickEnabled" Value="True" />
<Setter Property="Width" Value="100" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Test" />
<Border
Grid.Row="1"
Background="Purple"
BorderBrush="Black"
BorderThickness="1"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
FontSize="16"
Content="Font Size:"/>
<TextBox
Grid.Column="1"
FontSize="16"
Text="{Binding ElementName=SliderFont, Path=Value, Mode=TwoWay}"
Width="50"
MaxLength="5" />
<Slider
Style="{DynamicResource SliderStyle}"
Grid.Column="2"
Name="SliderFont" />
</Grid>
</Border>
</Grid>
</Window>
same idea but using a usercontrol.
<Window x:Class="TestApp3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp3"
Title="Window1">
<Window.Resources>
<Style x:Key="SliderStyle" TargetType="{x:Type Slider}">
<Setter Property="Value" Value="10" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Interval" Value="1" />
<Setter Property="Minimum" Value="5" />
<Setter Property="Maximum" Value="50" />
<Setter Property="TickFrequency" Value="0.25" />
<Setter Property="IsSnapToTickEnabled" Value="True" />
<Setter Property="Width" Value="100" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--<TextBox Grid.Row="0" Text="Test" />-->
<local:myusercontrol Grid.Row="0" />
<Border
Grid.Row="1"
Background="Purple"
BorderBrush="Black"
BorderThickness="1"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
FontSize="16"
Content="Font Size:"/>
<TextBox
Grid.Column="1"
FontSize="16"
Text="{Binding ElementName=SliderFont, Path=Value, Mode=TwoWay}"
Width="50"
MaxLength="5" />
<Slider
Style="{DynamicResource SliderStyle}"
Grid.Column="2"
Name="SliderFont" />
</Grid>
</Border>
</Grid>
</Window>
The usercontrol
<UserControl x:Class="TestApp3.myusercontrol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TextBox Text="Test" />
</Grid>
</UserControl>
The usercontrols textbox fontsize isnt growing at all. the reason i want to get this working is cause i'd like to put somthing like it in our themes so we wont have to worry about it later on. I've been tinkering with this for far too long. Any ideas on how to get this working would be great.
i know i can pass along the FontSize value in the usercontrol but i'd like to be able to control more than one controls FontSize.
Hope this makes sense,
~Boots
OK, it took me a little while, but I finally got this working.
You need to create a dependency property in your user control (this is in the code behind - C# in this case):
public static readonly DependencyProperty UCFontSizeProperty = DependencyProperty.Register(
"UCFontSize", typeof(double), typeof(myusercontrol));
public double UCFontSize
{
get { return (double)this.GetValue(UCFontSizeProperty); }
set { this.SetValue(UCFontSizeProperty, value); }
}
Then in the user control XAML have:
<UserControl x:Class="TestApp3.myusercontrol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="TextBoxUserControl"
Height="200" Width="250">
<Grid>
<TextBox Text="Test" FontSize="{Binding ElementName=TextBoxUserControl, Path=UCFontSize}" x:Name="UCTextBox" />
</Grid>
</UserControl>
Then add another Style setter:
<Style TargetType="{x:Type local:U myusercontrol}">
<Setter Property="UCFontSize" Value="{Binding ElementName=SliderFont, Path=Value}" />
</Style>
You don't need to change how you instantiate the user control on your main page.
I got it to work using an XMLfile as a resource.
Just add this to your resources
<XmlDataProvider x:Key="XmlFontFile" Source="pack://application:,,,/TestApp3;component/XMLFile1.xml" />
and this to your SliderStyle
<Setter Property="Value" Value="{Binding Source={StaticResource XmlFontFile}, XPath=Style/TextBoxFontSize, Mode=TwoWay}" />
and this to your TextBoxStyle
<Setter Property="FontSize" Value="{Binding Source={StaticResource XmlFontFile}, XPath=Style/TextBoxFontSize}" />
my xmlfile looks like
<?xml version="1.0" encoding="utf-8" ?>
<Style>
<TextBoxFontSize>16</TextBoxFontSize>
</Style>
Related
I started doing the UI for an app in WinForm but I just read about WPF and I decided to re-do my work on it. But since is a new think I ran in a small problem:
As you can see in the screenshot bellow I have 3 vertical buttons that are centered on the main form. How can I achieve the same thing in WPF? I've tried any value I could find to change position but all of them failed.
Here is my main form XAML:
<Window x:Class="YouTubeMusicPlayerWpf.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:YouTubeMusicPlayerWpf"
xmlns:fa="http://schemas.fontawesome.io/icons/"
mc:Ignorable="d" Height="500" Width="900" Background="Black" WindowStyle="None" AllowsTransparency="true" WindowStartupLocation="CenterScreen" Name="mainWindow">
<WindowChrome.WindowChrome>
<WindowChrome ResizeBorderThickness="6" CornerRadius="0" GlassFrameThickness="0">
</WindowChrome>
</WindowChrome.WindowChrome>
<StackPanel Background="#FF201E2B">
<!-- TitleBar buttons -->
<Grid HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal">
<Button fa:Awesome.Content="WindowMinimize" Background="Transparent" Foreground="White" VerticalAlignment="Stretch" Padding="8" BorderThickness="0" WindowChrome.IsHitTestVisibleInChrome="True" Name="minButton" Click="MinButton_OnClick"></Button>
<Button fa:Awesome.Content="WindowMaximize" Background="Transparent" Foreground="White" VerticalAlignment="Stretch" Padding="8" BorderThickness="0" WindowChrome.IsHitTestVisibleInChrome="True" Name="maxButton" Click="MaxButton_OnClick"></Button>
<Button fa:Awesome.Content="WindowClose" Background="Transparent" Foreground="White" VerticalAlignment="Stretch" Padding="8" BorderThickness="0" WindowChrome.IsHitTestVisibleInChrome="True" Name="closeButton" Click="CloseButton_OnClick"></Button>
</StackPanel>
</Grid>
<!-- Menu Buttons-->
<Grid HorizontalAlignment="Left" VerticalAlignment="Center">
<StackPanel Orientation="Vertical">
<Button fa:Awesome.Content="Youtube" FontSize="40" Background="#00000000" Foreground="White" BorderBrush="Transparent" Width="80" Height="80" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
<Button fa:Awesome.Content="Music" FontSize="40" Background="#00000000" Foreground="White" BorderBrush="Transparent" Width="80" Height="80" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
<Button fa:Awesome.Content="Download" FontSize="40" Background="#00000000" Foreground="White" BorderBrush="Transparent" Width="80" Height="80" VerticalAlignment="Center" HorizontalAlignment="Center"></Button>
</StackPanel>
</Grid>
</StackPanel>
</Window>
The problem is your root element. Replace the StackPanel by a grid and problem solved.
Bonus : if you are new to WPF, I show you how to refactorize your styles.
<Grid Background="Black">
<Grid.Resources>
<!-- Exemple of a style with a key to reuse -->
<Style x:Key="LeftButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="40" />
<Setter Property="Width" Value="80" />
<Setter Property="Height" Value="80" />
<Setter Property="Background" Value="#00000000" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="Transparent" />
</Style>
</Grid.Resources>
<!-- TitleBar buttons -->
<StackPanel
HorizontalAlignment="Right"
VerticalAlignment="Top"
Orientation="Horizontal">
<StackPanel.Resources>
<!-- Exemple of a implicit styles that will be set to each child with a matching type -->
<!-- Notice there is no x:Key -->
<Style TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="8" />
<Setter Property="BorderThickness" Value="0" />
</Style>
</StackPanel.Resources>
<Button Content="b1" />
<Button Content="b2" />
<Button Content="b3" />
</StackPanel>
<!-- Menu Buttons -->
<StackPanel
HorizontalAlignment="Left"
VerticalAlignment="Center"
Orientation="Vertical">
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
</StackPanel>
</Grid>
Notice there is neither RowDefinitions nor ColumnDefinitions.
Here is how to do the same thing with place to set the content (a red box for exemple).
<Grid Background="Black">
<Grid.Resources>
<!-- Exemple of a style with a key to reuse -->
<Style x:Key="LeftButtonStyle" TargetType="Button">
<Setter Property="FontSize" Value="40" />
<Setter Property="Width" Value="80" />
<Setter Property="Height" Value="80" />
<Setter Property="Background" Value="#00000000" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="Transparent" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!-- TitleBar buttons -->
<StackPanel
Grid.Column="1"
HorizontalAlignment="Right"
Orientation="Horizontal">
<StackPanel.Resources>
<!-- Exemple of a implicit styles that will be set to each child with a matching type -->
<!-- Notice there is no x:Key -->
<Style TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Padding" Value="8" />
<Setter Property="BorderThickness" Value="0" />
</Style>
</StackPanel.Resources>
<Button Content="b1" />
<Button Content="b2" />
<Button Content="b3" />
</StackPanel>
<!-- Menu Buttons -->
<StackPanel
Grid.RowSpan="2"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Orientation="Vertical">
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
<Button Content="icon" Style="{StaticResource LeftButtonStyle}" />
</StackPanel>
<Grid Background="Red" Grid.Row="1" Grid.Column="1"/>
</Grid>
I have used this xaml code to modify the background color of my DataGrid Header and the column separator visibility. My problem is that the column separators are elminated so the column headers look a little bit different:
this is my code :
<DataGrid AutoGenerateColumns="False" Grid.ColumnSpan="9" Grid.Row="1" Height="82" HorizontalAlignment="Left" Margin="99,427,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="363" Background="#9DB9EB" >
<DataGrid.Columns>
<DataGridTextColumn Header="Header1" />
<DataGridTextColumn Header="Header2" />
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#cee8ef" />
<Setter Property="BorderThickness" Value="2" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
How can I make the header column separtor visible?
Try this style
<Style TargetType="{x:Type DataGridColumnHeader}"
>
<Setter Property="Background"
Value="#cee8ef" />
<Setter Property="BorderThickness"
Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Content="{TemplateBinding Content}"
Grid.Column="0"
HorizontalAlignment="Center"
Margin="10,0,25,0" />
<Thumb HorizontalAlignment="Right"
Grid.Column="1"
Name="PART_HeaderGripper"
Margin="0,4,0,4"
Width="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Hope it helps you, you can modify it further if you need it ...
I have a ListView with custom ItemContainer and ItemTemplate. ItemTemplate contains, among other controls, a button. Whenever I click or tap a button ListView a whole item is selected, in contrast to the expected button click or tap event to be fired. I've noticed that the only time the button is clickable is when I position mouse cursor at the top border of the button (whichi is also the only time I am getting the default mouse over effect).
Here is the XAML:
<Style TargetType="ListView" x:Key="SalesOrdersListViewStyle">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
<!--<Setter Property="Background" Value="Transparent"/>-->
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="Padding" Value="12,6"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
Foreground="{StaticResource HighlightPressedBrush}"
CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="Inline"
ContentTransitions="{TemplateBinding ContentTransitions}"
CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
PointerOverForeground="{StaticResource HighlightPressedBrush}"
PressedBackground="{ThemeResource HighlightAlternativePressedBrush}"
PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
PointerOverBackground="{ThemeResource HighlightPointerOverBrush}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
SelectedPressedBackground="{ThemeResource ControlBackgroundDarkBrush}"
SelectionCheckMarkVisualEnabled="True"
SelectedForeground="#FFFFFFFF"
SelectedPointerOverBackground="{ThemeResource HighlightAlternativePointerOverBrush}"
SelectedBackground="{ThemeResource HighlightPressedBrush}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
x:Uid="SalesOrderNumber"
Grid.Row="0" Grid.Column="1" />
<TextBlock
Grid.Row="0" Grid.Column="2"
Text="{Binding Number}" />
<!-- TEST BUTTON THAT CANNOT BE TAPPED -->
<Button
Grid.Row="0" Grid.RowSpan="4" Grid.Column="3"
HorizontalAlignment="Left" VerticalAlignment="Center"
Background="Red"
IsHitTestVisible="True"
Content="TEST" />
<TextBlock
x:Uid="DeliveryMode"
Grid.Row="1" Grid.Column="1" />
<TextBlock
Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2"
Text="{Binding DeliveryMode}" /
<TextBlock
x:Uid="ShippingDate"
Grid.Row="2" Grid.Column="1" />
<TextBlock
Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"
Text="{Binding ShippingDate}" />
<TextBlock
x:Uid="ProjectNumber"
Grid.Row="3" Grid.Column="1" />
<TextBlock
Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2"
Text="{Binding ProjectNumber}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
How do I make the button click or tap event to fire instead of selecting an item?
Note: I just commented out the entire <Setter Property="ItemContainerStyle"> and the issue is still there.
The problem is in you TextBlocks with Grid.Column="2" Grid.ColumnSpan="2". Set ColumSpan to 1 and it will work.
I am a total newbie but couldn't you just add a Button click event?
<Button
Grid.Row="0" Grid.RowSpan="4" Grid.Column="3"
HorizontalAlignment="Left" VerticalAlignment="Center"
Background="Red"
IsHitTestVisible="True"
Click"btn_Click"
Content="TEST" />
And in the code:
private void btn_Click(object sender, RoutedEventArgs e)
{
// Do stuff
}
I hope I don't talk bullsh*t here but I don't see your click event anywhere.
I'm just begin study WPF, so I'm unfamiliar with style and template.
I want to customize a CheckBox with a Image and two Labels like this:
How can I do?
.xaml
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<CheckBox Width="150"
Height="40"
Margin="4"
Padding="4,0,0,0">
<Grid Background="#FFEEEEEE"
Width="130"
MaxWidth="Infinity">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="0"
Margin="5"
Source="/WpfApplication4;component/Images/LargeIcon.png" />
<Label Grid.Row="0"
Grid.Column="1"
Padding="0">
Label1
</Label>
<Label Grid.Row="1"
Grid.Column="1"
Padding="0">
Label2
</Label>
</Grid>
</CheckBox>
</StackPanel>
</Window>
Edit:
.xaml
<Application x:Class="WpfApplication4.App"
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/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="MyCheckBox"
TargetType="{x:Type CheckBox}">
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Padding" Value="4,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<DockPanel Background="#FFEEEEEE"
Height="34"
Width="130">
<Image DockPanel.Dock="Left"
Source="/WpfApplication4;component/Images/LargeIcon.png"
Margin="5" />
<TextBlock DockPanel.Dock="Top" Text="Label1" />
<TextBlock DockPanel.Dock="Top" Text="Label2" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
used in .xaml
<CheckBox Style="{StaticResource ResourceKey=MyCheckBox}" />
Something be presented, but the small grid is disappeared, like this:
How can I do?
The DockPanel may be the best option for this layout
Example:
<CheckBox>
<DockPanel Height="34">
<Image DockPanel.Dock="Left" Source="/WpfApplication4;component/Images/LargeIcon.png" Margin="2" />
<TextBlock DockPanel.Dock="Top" Text="Label1" />
<TextBlock DockPanel.Dock="Top" Text="Label2" />
</DockPanel>
</CheckBox>
Edit:
It looks like you still want to use the default Checkbox Template but just override the Content in your Style.
Example:
<Style x:Key="MyCheckBox" TargetType="{x:Type CheckBox}">
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Padding" Value="4,0,0,0"/>
<Setter Property="Content">
<Setter.Value>
<DockPanel Background="#FFEEEEEE" Width="130" MaxWidth="Infinity">
<Image DockPanel.Dock="Left" Source="Capture.png" Margin="5" />
<TextBlock DockPanel.Dock="Top" Text="Label1" />
<TextBlock DockPanel.Dock="Top" Text="Label2" />
</DockPanel>
</Setter.Value>
</Setter>
</Style>
Result:
I have a Usercontrol: SnazzyForm, which in addition to a couple of borders and and a header area and whatnot, also has a Content Presenter which under .NET Framework 3.5 presented content passed to it in other forms that used the control just fine.
However after moving the project to 4.0 I am greeted with "Cannot add content to object of type" blah blah blah.
The codebehind for the control is thus:
Imports System
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.ComponentModel
Public Class SnazzyForm
Inherits ContentControl
Public Shared TitleProperty As DependencyProperty = DependencyProperty.Register("Title", GetType(String), GetType(SnazzyForm))
<Description("Title to display"), _
Category("Custom")> _
Public Property Title() As String
Get
Return CType(GetValue(TitleProperty), String)
End Get
Set(ByVal value As String)
SetValue(TitleProperty, value)
End Set
End Property
Shared Sub New()
' Insert code required on object creation below this point.
DefaultStyleKeyProperty.OverrideMetadata(GetType(SnazzyForm), New FrameworkPropertyMetadata(GetType(SnazzyForm)))
End Sub
End Class
The template in the resourcedictionary that governs this control is thus:
<Style TargetType="{x:Type local:SnazzyForm}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SnazzyForm}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type Border}" x:Key="formOuterBorderStyle">
<Setter Property="Margin" Value="22,22,22,3" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderBrush" Value="{StaticResource BrushBorder}" />
<Setter Property="BorderThickness" Value="3" />
<Setter Property="Background" Value="{StaticResource BrushBackgroundDark}" />
<Setter Property="CornerRadius" Value="20,20,0,0" />
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="formTitleStyle">
<Setter Property="Foreground" Value="{StaticResource BrushWhiteSmoke}" />
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Padding" Value="11,7,7,7" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="formBreadcrumbStyle">
<Setter Property="Foreground" Value="{StaticResource BrushWhiteSmoke}" />
<Setter Property="FontFamily" Value="Verdana" />
<Setter Property="FontSize" Value="11" />
<Setter Property="Padding" Value="7" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
<Style TargetType="{x:Type Rectangle}" x:Key="formBackgroundRectangleStyle">
<Setter Property="Fill" Value="{StaticResource BrushABCLight}" />
</Style>
<Style TargetType="{x:Type Border}" x:Key="formTitleBorderStyle">
<Setter Property="BorderBrush" Value="{StaticResource BrushBlack}" />
<Setter Property="BorderThickness" Value="0,0,0,2" />
</Style>
</ControlTemplate.Resources>
<AdornerDecorator d:DesignWidth="640" d:DesignHeight="480">
<Grid Width="Auto" Height="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" FontFamily="Arial" FontSize="18.667" Foreground="#FFE0E0E0" Text="{TemplateBinding Title}" TextWrapping="Wrap" Panel.ZIndex="2" Visibility="Collapsed"/>
<Path Fill="Black" Stretch="Fill" Stroke="{x:Null}" Margin="0" VerticalAlignment="Stretch" Height="Auto" Grid.Column="1" Grid.Row="0" Data="M0.5,0.5 L39.5,39.5 0.5,39.5 z"/>
<Border Background="Black" Margin="0" VerticalAlignment="Stretch" Height="Auto" Grid.Row="0" CornerRadius="10,0,0,0"/>
<!-- <Rectangle Fill="Black" Stroke="Black" Margin="0" VerticalAlignment="Stretch" Height="Auto" Grid.Row="0"/> -->
<Rectangle Stroke="{x:Null}" Margin="0" Height="Auto" Opacity="0.5" Grid.ColumnSpan="2" Grid.Row="3" Grid.RowSpan="1">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF222222" Offset="0"/>
<GradientStop Color="#FFB3B3B3" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Fill="Black" Stroke="Black" Margin="0" Grid.ColumnSpan="2" Grid.Row="1"/>
<Rectangle Fill="{TemplateBinding Background}" Stroke="Black" Margin="0" Height="Auto" Panel.ZIndex="-7" Grid.ColumnSpan="2" Grid.Row="2" Grid.RowSpan="1"/>
<Border x:Name="bdr" Style="{StaticResource formOuterBorderStyle}" Margin="0" Grid.ColumnSpan="2" Grid.RowSpan="3" d:LayoutOverrides="Width, Height" d:IsHidden="True" Visibility="Collapsed"/>
<Grid Margin="3" Grid.ColumnSpan="2" Grid.RowSpan="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="77" />
<RowDefinition MinHeight="400" />
</Grid.RowDefinitions>
<Border Style="{StaticResource formTitleBorderStyle}" BorderBrush="{x:Null}">
<StackPanel Margin="0">
<TextBlock Text="{Binding ViewModelFriendlyName}" Style="{StaticResource formTitleStyle}" />
<TextBlock Margin="11,0,0,0" Text="{Binding BreadcrumbTrail}" Style="{StaticResource formBreadcrumbStyle}" />
</StackPanel>
</Border>
<Rectangle Grid.Row="1" Style="{StaticResource formBackgroundRectangleStyle}" Visibility="Collapsed" />
<!--Put the form below here-->
<ContentPresenter x:Name="ContentPresenterX" Grid.Row="1" Margin="0" />
<!--Put the form above here-->
</Grid>
</Grid>
</AdornerDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I guess I really just want to know why I now have to tell the control to use the contentpresenter to present the content, when previously it know that is what I wanted, and also how I do it.
Any thoughts?
Cory
Added Content={TemplateBinding Property=Content} to ContentPresenter.
MSDN Forum post regarding this issue suggested adding the ContentPropertyAttribute setter to the controls codebehind, but I could not get that to work.
Cory