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 change the BarCheckITem background color, am having hardtime changing the styles for devexpress controls
<dxb:ToolBarControl ShowBackground="True" Grid.Row="0" HorizontalAlignment="Stretch"
VerticalAlignment="Top"
AllowCustomizationMenu="True"
BarItemDisplayMode="ContentAndGlyph" UseWholeRow="True"
AllowHide="False" AllowQuickCustomization="False" RotateWhenVertical="False">
<dxb:BarCheckItem Content="Forms"
Glyph="{dx:DXImage Image=AddItem_16x16.png}"
GroupIndex="-11"
BarItemDisplayMode="ContentAndGlyph"
LargeGlyph="{dx:DXImage Image=AddItem_32x32.png}" />
<dxb:ToolBarControl>
You need to override the BarCheckItemLink.CustomResources and then to add a style to override the default template. I have made a simple sample to show this:
<dx:DXWindow x:Class="BarCheckItemBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
Title="MainWindow" Height="350" Width="525">
<Grid>
<dxb:BarManager ToolbarGlyphSize="Large">
<dxb:BarManager.Items>
<dxb:BarCheckItem x:Name="ItemNormal"
Content="I am normal"
Glyph="{dx:DXImage Image=AddItem_16x16.png}"
BarItemDisplayMode="ContentAndGlyph"
LargeGlyph="{dx:DXImage Image=AddItem_32x32.png}" />
<dxb:BarCheckItem x:Name="ItemNotNormal"
Content="I am not normal lol"
Glyph="{dx:DXImage Image=AddItem_16x16.png}"
GroupIndex="-11"
BarItemDisplayMode="ContentAndGlyph"
LargeGlyph="{dx:DXImage Image=AddItem_32x32.png}" />
</dxb:BarManager.Items>
<dxb:BarManager.Bars>
<dxb:Bar>
<dxb:Bar.DockInfo>
<dxb:BarDockInfo ContainerType="Top"/>
</dxb:Bar.DockInfo>
<dxb:BarCheckItemLink BarItemName="ItemNormal" />
<dxb:BarCheckItemLink BarItemName="ItemNotNormal">
<dxb:BarCheckItemLink.CustomResources>
<ResourceDictionary>
<Style TargetType="{x:Type dxb:BarCheckItemLinkControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type dxb:BarItemLinkControl}">
<Grid>
<Border Background="Yellow"/>
<dxb:BarItemLayoutPanel x:Name="PART_LayoutPanel"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</dxb:BarCheckItemLink.CustomResources>
</dxb:BarCheckItemLink>
</dxb:Bar>
</dxb:BarManager.Bars>
</dxb:BarManager>
</Grid>
</dx:DXWindow>
And the output window:
Hope this helps
I am a beginner with Visual Studio.
I want to have separately defineable icons in my buttons. I want to achieve this using only XAML in order to keep my GUI stuff as separate as possible.
I'd like to be able to use it like this:
<Button x:Name="CallButton" Height="128px" Width="128px"
Style="{DynamicResource RoundButton}" Content="{StaticResource PhoneIcon}">
I have defined RoundButton and PhoneIcon in their respective Resource Dictionaries.
RoundButton:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="RoundButton" TargetType="{x:Type Button}">
<Setter Property="Content" Value="{Binding Grid}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="100" BorderThickness="2" x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}">
<Grid>
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"
x:Name="contentPresenter" Opacity="1" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
PhoneIcon:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas x:Key="PhoneIcon">
<Path Stroke="Gray" Data="m 492.438 397.75 -2.375 -7.156 c -5.625 -16.719 -24.063 -34.156 -41 -38.75 l -62.688 -17.125 c -17 -4.625 -41.25 1.594 -53.688 14.031 L 310 371.438 C 227.547 349.157 162.891 284.5 140.641 202.063 l 22.688 -22.688 c 12.438 -12.438 18.656 -36.656 14.031 -53.656 L 160.266 63 C 155.641 46.031 138.172 27.594 121.485 22.031 l -7.156 -2.406 c -16.719 -5.563 -40.563 0.063 -53 12.5 L 27.391 66.094 c -6.063 6.031 -9.938 23.281 -9.938 23.344 -1.187 107.75 41.063 211.562 117.281 287.781 76.031 76.031 179.453 118.219 286.891 117.313 0.563 0 18.313 -3.813 24.375 -9.844 l 33.938 -33.938 c 12.437 -12.437 18.062 -36.281 12.5 -53 z" />
</Canvas>
</ResourceDictionary>
I have merged my Resource Dictionaries in the App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="RoundButton.xaml" />
<ResourceDictionary Source="Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
My problem is that while I get the image displayed, it's disproportionately big. I have tried pretty many solutions so I'm starting to lose track of the tried with my trial-and-error approach.
How to get the image show right in the center of the button with correct size?
NB I have trimmed some of the code I'm posting here - but I have also tested the code I'm posting here and the problem persists.
Thanks in advance!
You may use a Grid instead of a Canvas in your PhoneIcon resource, and set the Path's Stretch property. This would scale the Path to a Size that fits the Button's bounds:
<Grid x:Key="PhoneIcon">
<Path Stretch="Uniform" ... />
</Grid>
You may even set the Grid size explicitly:
<Grid x:Key="PhoneIcon" Width="80">
<Path Stretch="Uniform" ... />
</Grid>
An even simpler solution would be to use the Path without any container:
<Path x:Key="PhoneIcon" Width="80" Stretch="Uniform" ... />
How can I change a Button template dynamically?
I have a ComboBox where by changing his selected value I want to change a Button Template.
This is what I have been trying to do:
<Window.Resources>
<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
<Grid>
<Rectangle Fill="#FF2D2D7A" Margin="7.5,9.5,8.5,11" Stroke="Black"
RadiusX="45" RadiusY="45" StrokeThickness="6"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="ButtonControlTemplate2" TargetType="{x:Type Button}">
<Grid>
<ed:RegularPolygon Fill="#FFE7F9C9" Height="Auto" InnerRadius="0.47211"
Margin="20.5,16,15.5,8" PointCount="5" Stretch="Fill"
Stroke="Black" StrokeThickness="6" Width="Auto"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ComboBox Name="GroupBoxHeaderComboBox" ItemsSource="{Binding Path=collection}"
DisplayMemberPath="Key" Height="52" Margin="211.5,60,230.5,0"
VerticalAlignment="Top" SelectedIndex="1"/>
<Button Content="Button" HorizontalAlignment="Left" Height="102" Margin="47.5,0,0,91"
VerticalAlignment="Bottom" Width="132"
Template="{DynamicResource ButtonControlTemplate2}"/>
<Button Content="Button" HorizontalAlignment="Right" Height="112.5" Margin="0,0,27.5,85"
VerticalAlignment="Bottom" Width="153"
Template="{DynamicResource ButtonControlTemplate1}"/>
<Button Content="Button" Height="102" Margin="239.5,0,252.5,13.5"
VerticalAlignment="Bottom"
Template="{Binding ElementName=GroupBoxHeaderComboBox, Path=SelectedItem.Value}"/>
</Grid>
And here are the associated Templates:
<Window.Resources>
<ControlTemplate x:Key="ButtonControlTemplate1" TargetType="{x:Type Button}">
<Grid>
<Rectangle Fill="#FF2D2D7A" Margin="7.5,9.5,8.5,11" Stroke="Black"
RadiusX="45" RadiusY="45" StrokeThickness="6"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="ButtonControlTemplate2" TargetType="{x:Type Button}">
<Grid>
<ed:RegularPolygon Fill="#FFE7F9C9" Height="Auto" InnerRadius="0.47211"
Margin="20.5,16,15.5,8" PointCount="5" Stretch="Fill"
Stroke="Black" StrokeThickness="6" Width="Auto"/>
</Grid>
</ControlTemplate>
</Window.Resources>
And the code behind:
public partial class MainWindow : Window
{
public Dictionary<string, string> collection
{
get;
private set;
}
public MainWindow()
{
this.InitializeComponent();
DataContext = this;
collection = new Dictionary<string, string>()
{
{ "DynamicResource ButtonControlTemplate2", "{DynamicResource ButtonControlTemplate2}"},
{ "DynamicResource ButtonControlTemplate1", "{DynamicResource ButtonControlTemplate2}"},
};
// Insert code required on object creation below this point.
}
}
Is there another genric way to acomplish this?... I want that most of the code would be xaml.
EDIT:
Is there a point to do it using a style? Let's say I want more then one object to act, otherwise is there a point to change the style and to do it all from there?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public Dictionary<string, ControlTemplate> collection
{
get
{
Dictionary<string, ControlTemplate> controlTemplates = new Dictionary<string, ControlTemplate>();
controlTemplates.Add("ButtonControlTemplate1", FindResource("ButtonControlTemplate1") as ControlTemplate);
controlTemplates.Add("ButtonControlTemplate2", FindResource("ButtonControlTemplate2") as ControlTemplate);
return controlTemplates;
}
}
}
Create a ControlTemplate in Windows resource,
<Window.Resources>
<ControlTemplate x:Key="GreenTemplate" TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="Green"/>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Window.Resources>
Now in run time you can change the template property of button.
private void Button_Clicked(object sender, RoutedEventArgs e)
{
Button btn = e.OriginalSource as Button;
if (btn != null)
{
btn.Template = FindResource("GreenTemplate") as ControlTemplate;
}
}
You can use a data trigger and do it all in xaml.
This uses a tree but the concept is the same
<Window x:Class="WpfBindingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfBindingTest"
Title="Window3" Height="300" Width="300" Name="win3" >
<Window.Resources>
<XmlDataProvider x:Key="treeData" XPath="*">
<x:XData>
<Items Name="Items" xmlns="">
<Item1/>
<Item2>
<Item22/>
<Item12/>
<Item13>
<Item131/>
<Item131/>
</Item13>
</Item2>
</Items>
</x:XData>
</XmlDataProvider>
<HierarchicalDataTemplate ItemsSource="{Binding XPath=child::*}" x:Key="template">
<TextBlock Name="textBlock" Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</Window.Resources>
<StackPanel>
<TreeView ItemTemplate="{StaticResource template}"
Name="treeView"
ItemsSource="{Binding Source={StaticResource treeData}}">
<TreeView.ItemContainerStyle>
<!--Using style setter to set the TreeViewItem.IsExpanded property to true, this will be applied
to all TreeViweItems when they are generated-->
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<Button Width="120" Height="30">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content" Value="Default" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=treeView, Path=SelectedItem.Name}" Value="Item12">
<Setter Property="Content" Value="Now changed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
from here.
(I just googled to get an example faster)
I am using Silverlight 4 and I am trying to integrate one of the themes from the Silverlight 4 toolkit from April.
My App.xaml reads as follows:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/System.Windows.Controls.Theming.ExpressionDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
When my main window reads
<Grid x:Name="LayoutRoot" Background="{Binding Source={StaticResource ThemeBackgroundBrush}}">
<TextBlock Text="Test" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{Binding Source={StaticResource ThemeForegroundBrush}}" />
</Grid>
It works perfectly. However I would like to use resources so I went ahead and did the following
<Grid x:Name="LayoutRoot" Background="{Binding Source={StaticResource ThemeBackgroundBrush}}">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding Source={StaticResource ThemeForegroundBrush}}" />
</Style>
</Grid.Resources>
<TextBlock Text="Test" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
It fails :-(
I have tried to place the resource style in other places like the App.xaml etc.
Anyone know how I can use resources so I don't have to specify the foreground for each TextBlock?
PS - I am using the ExpressionDark theme...
Thanks in Advance,
Mike
I don't believe that you need the Binding Source part.
I have used the following
<Setter Property="Foreground" Value="{StaticResource ThemeForegroundBrush}" />
in the past and it works well.