Split TabItem cause style lost - wpf

I've a TabControl with this structure:
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</TabControl.Resources>
<local:Analysis />
</TabControl>
Now I need to split each TabItem, so I've create something like a UserControl with this structure:
<TabItem 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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MetroTabItem}">
<Grid>
</Grid>
the problem's that the TabItem imported with namespace local: doesn't take the style of the TabControl in the Resource.
So for solve this I need to put the style resource in each TabItem, this will cause redundancy code.
How can I take the style of TabControl Resource in the splitted TabItem?
Thanks.

You can create your own style as a static resource in your App.xaml or your Window.xaml like this:
<Application.Resources>
<Style TargetType="TabItem" x:Key="MyTabItemStyle" BasedOn="{StaticResource MetroTabItem}">
<Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20" />
</Style>
</Application.Resources>
You give your style a name (MyTabItemStyle in this example) which is based on the MetroTabItem style.
Then, instead of using the MetroTabItem style directly in your custom TabItem, you use your own style MyTabItemStyle like this:
<TabItem 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:Controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d" Header="Analysis" Style="{StaticResource MyTabItemStyle}">
<Grid>
</Grid>
</TabItem>

Related

Resource "x" could not be resolved WPF

Im new at WPF and im using some DevExpress controls
Im trying to implement an style to some buttons but always shows the error that the Resource cannot be resolved.
MainWindow:
<dx:DXWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" dx:ThemeManager.ThemeName="Office2016"
Title="MainWindow" Height="746.218" Width="1139.154"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="1" >
<dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=CustomStyles}" Width="55" ToolTip="Refresh" Margin="10,10,10,10" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" />
<dx:SimpleButton x:Name="btndNew" Width="55" ToolTip="New Customer" Margin="10" Glyph="{dx:DXImage Image=NewContact_32x32.png}" Content="New Customer" />
<dx:SimpleButton x:Name="btnDelete" ToolTip="Delete Customer" Width="55" Margin="10" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/>
</StackPanel>
</Grid>
This is the App.xaml
<Application x:Class="LicenceManagerWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="OnAppStartup_UpdateThemeName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="CustomStyles" Source="StyleResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This is my Styles file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LicenceManagerWPF"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Style x:Name="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
I was searching and everything looks like fine but i dont get it why it cannot be resolved.
Regards
There is no need to name ResourceDictionary , just provide its Source:
<ResourceDictionary Source="StyleResource.xaml"/>
ResourceDictionary items should have x:Key. In case of a Style if there is no explicit key, TargetType will be used as a key. It is a way to create default styles.
If you want named style, then set x:Key
<Style x:Key="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
And finally StaticResource extension references resources by resource key, not by names:
Style="{StaticResource HeaderButtons}"
Also: when you set a Style for a button, Margin and Width settings (Width="55" Margin="10,10,10,10") becomes redundant. They can be used to override style setting, but in this case they are the same so why write them?

how to disable cut, copy paste and right click across all WPF forms

We are building a WPF kiosk application.
We need to disable CUT COPY PASTE and RIGHT-CLICKs
Please how can this be done?
This SO post does not give a centralized solution for all forms:
How to suppress Cut, Copy and Paste Operations in TextBox in WPF?
You need to add a Style in your App.xaml in which you define:
<Style TargetType="TextBox">
<!-- OR -->
<Style TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>
But this will only work for the Items that are NOT in a DataTemplate.
UPDATE:
App.xaml:
<Application x:Class="TestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="ContextMenu" Value="{x:Null}"/>
</Style>
</Application.Resources>
And here is the MainWindow.xaml:
<Window x:Class="TestApp.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"
xml:lang="en-GB"
xmlns:local="clr-namespace:TestApp"
xmlns:converter="clr-namespace:TestApp.Converters"
mc:Ignorable="d"
Height="478.889" Width="903.889">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel>
<TextBox Name="txtBx" MinHeight="150"
VerticalAlignment="Top" AutoWordSelection="True"
MaxLines="10"
TextWrapping="WrapWithOverflow"
SelectionChanged="txtBx_TextHighlighted"
ToolTip="{x:Null}"
Margin="10"/>
</StackPanel>
</Grid>
If you Right Click on the TextBox you will not have any ContextMenu available to you.
UPDATE 2:
Continuing from our chat, the TextBox was referencing other styles which were overriding whatever we set in the App.xaml. As the external styles were loaded after the App.xaml.

How to resuse the same instance of a style

I’m just testing some XAML stuff, and I am wondering why a style which comes from a resource dictionary is created multiple times when used inside a data template for listbox.
Let me give you an example
<UserControl x:Class="WpfApplication1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox ItemTemplate="{DynamicResource foo}" ItemsSource="{Binding Names}"/>
</UserControl>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="foo">
<TextBlock Style="{StaticResource TextBlockStyle}" Text="{Binding}" Loaded="TextBlock_Loaded"/>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<wpfApplication1:UserControl1 />
</Window>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Pink" />
</Style>
</ResourceDictionary>
Now I’ve done a simple test in code behind to see whether the textblocks get the same style,
and it is surprising that each one of them gets a new instance of the same style, so if I had 50 records then I would have 50 instances of the same style.
Is that how people do it, or is there a pattern to reuse the same instance as many times as needed?
Thanks for looking :)

How to write styles to callouts wpf

I'm using the next namespace (http://schemas.microsoft.com/expression/2010/drawing).
I've two controls for the collout which use the same styles.
i want to write some reusable style for those two controls, but no success...
Any suggestions?
You can reuse styles, whether it's for callouts or any other control, by making them resources and referencing them later. For example:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
x:Class="WpfApplication1.MainWindow">
<Window.Resources>
<Style x:Key="MyCalloutStyle" TargetType="ed:Callout">
<Setter Property="Fill" Value="Orange" />
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ed:Callout Style="{StaticResource MyCalloutStyle}" />
<ed:Callout Style="{StaticResource MyCalloutStyle}" />
</Grid>
</Window>

WPF custom control style problem

I have a custom control (from MS Toolkit - DatePicker). I've made my own style like this:
<Style TargetType="{x:Type local:DatePicker}">
But this style does not apply automatically. I have to add Key:
<Style x:Key="DatePickerStyle" TargetType="{x:Type local:DatePicker}">
and reference it in each custom control like
<toolkit:DatePicker Style="{StaticResource DatePickerStyle}"
...
to get it working. Does anyone know why?
have you tried changing the TargetType to:
TargetType="{x:Type toolkit:DatePicker}">
You are referencing local in one place and toolkit in another.
update:
I've tried it in a small app. This is the xaml as it should work:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication3.Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480"
xmlns:Toolkit="http://schemas.microsoft.com/wpf/2008/toolkit">
<Window.Resources>
<Style TargetType="{x:Type Toolkit:DatePicker}">
<Setter Property="Background" Value="#FFFF0000"/>
</Style>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Toolkit:DatePicker HorizontalAlignment="Left"
Margin="61,143,0,116" Width="232" />
</Grid>
</Window>
This example should create a datepicker with a red background.

Resources