How to add a DataTrigger programmatically - c#

I need to add a DataTrigger dynamically based on user interaction with a search function, so it cannot be done in XAML. However, the basic aim of what I am trying to do can be seen from the XAML code below. It needs to change the visibility of a button based on a ToggleButton's IsChecked state.
XAML:
<ToggleButton Name="myToggleButton" />
<Button Name="myButton">
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myToggleButton, Path=IsChecked}"
Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=myToggleButton, Path=IsChecked}"
Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button>
This is what I have in C# to try to produce the same functionality of the above XAML.
C#:
ToggleButton myToggleButton = new ToggleButton();
Button myButton = new Button();
Style style = new Style( typeof( Button ) );
DataTrigger tbChecked = new DataTrigger()
{
Binding = new Binding( "IsChecked" ) { Source = myToggleButton },
Value = true
},
tbNotChecked = new DataTrigger()
{
Binding = new Binding( "IsChecked" ) { Source = myToggleButton },
Value = false
};
tbChecked.Setters.Add( new Setter( Button.VisibilityProperty, Visibility.Visible ) );
tbNotChecked .Setters.Add( new Setter( Button.VisibilityProperty, Visibility.Collapsed ) );
style.Triggers.Add( tbChecked );
style.Triggers.Add( tbNotChecked );
myButton.Style = style;
For the life of me, I have not been able to figure out why it won't work. What am I missing?

maybe, you can try this:
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="b2v"/>
</StackPanel.Resources>
<CheckBox x:Name="switchCheckBox" Content="Show/Hide" VerticalAlignment="Center"/>
<TextBlock Text=" "/>
<Button Content="Test Button" Visibility="{Binding ElementName=switchCheckBox, Path=IsChecked, Converter={StaticResource b2v}}" Padding="25,5"/>
</StackPanel>

Related

Resource style xaml to c#

I have style resource in my button to make it rounded, and i want to create it using c#(code behind), how do i do it?
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Button.Resources>
Create the Style:
Style style = new Style() { TargetType = typeof(Border) };
style.Setters.Add(new Setter() { Property = Border.CornerRadiusProperty, Value = new CornerRadius(5) });
style.Seal();
Add it to the Button:
button.Resources.Add(typeof(Border), style);
XAML:
<Button x:Name="button" Content="..." />

WPF Custom Base Window Class and Style

I have a an app that will open many windows, and I want all windows to look the same. I am overriding the default Windows window chrome style and making my own, so any new window that is opened (excluding messageboxes) should have the same window style. However, no matter what I seem to try it does not work. I can get it to work with one window, but when I want to make it a global style it always crashes or simply doesn't work as it should.
Here is my code:
WindowBaseStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Styles"
xmlns:views="clr-namespace:Myproject.Views">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlobalStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type views:WindowBase}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="AllowsTransparency" Value="False" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="WindowState" Value="Normal" />
<Setter Property="WindowStyle" Value="SingleBorderWindow" />
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
UseAeroCaptionButtons="False"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type views:WindowBase}">
<Border BorderBrush="Blue" BorderThickness="1" SnapsToDevicePixels="True">
<DockPanel Background="White" LastChildFill="True" >
<Grid Background="Blue" DockPanel.Dock="Top">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Center">
<Button Name="PART_SystemMenuButton" Command="{Binding MenuCommand}" Style="{DynamicResource SystemIconButton}">
<Image Height="16" Width="16" Source="/Resources/icon.png" Stretch="Fill"/>
</Button>
<Viewbox Height="16" HorizontalAlignment="Stretch" Margin="14,2,0,0" >
<TextBlock FontSize="12" Foreground="White" Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" />
</Viewbox>
</StackPanel>
<StackPanel Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">
<Button x:Name="PART_MinimizeButton" Command="{Binding MinimizeCommand}" Height="30" Margin="0,0,0,0" ToolTip="Minimize" Width="45">
<Image Source="/Resources/minimize.png" Stretch="None" />
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#64AEEC"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="PART_MaximizeButton" Command="{Binding MaximizeCommand}" Height="30" Margin="0,0,0,0" Width="45">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="Source" Value="/Resources/maximize.png" />
<Setter Property="Stretch" Value="None" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Source" Value="/Resources/unmaximize.png" />
<Setter Property="Stretch" Value="None" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#64AEEC"/>
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="ToolTip" Value="Maximize" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="ToolTip" Value="Restore Down" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="PART_CloseButton" Command="{Binding CloseCommand}" Height="30" Margin="0,0,0,0" ToolTip="Close" Width="45" >
<Image Source="/Resources/close.png" Stretch="None" />
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</StackPanel>
</Grid>
<!-- this ContentPresenter automatically binds to the content of the window -->
<ContentPresenter />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
WindowBase.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
namespace MyProject.Views
{
[TemplatePart(Name = "PART_MinimizeButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_MaximizeButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_CloseButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_SystemMenuButton", Type = typeof(Button))]
public class WindowBase: Window
{
static WindowBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow)));
}
public WindowBase()
{
Loaded += (sender, evnt) =>
{
var MinimizeButton = (Button)Template.FindName("PART_MinimizeButton", this);
var MaximizeButton = (Button)Template.FindName("PART_MaximizeButton", this);
var CloseButton = (Button)Template.FindName("PART_CloseButton", this);
var SystemMenuButton = (Button)Template.FindName("PART_SystemMenuButton", this);
MinimizeButton.Click += (s, e) => WindowState = WindowState.Minimized;
MaximizeButton.Click += (s, e) => WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
CloseButton.Click += (s, e) => Close();
SystemMenuButton.Click += (s, e) => SystemCommands.ShowSystemMenu(this, GetMousePosition());
};
}
}
}
Window1.xaml
<local:WindowBase x:Class="MyProject.Views.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Views"
Height="750"
Width="1125">
<Grid>
</Grid>
</local:WindowBase>
Window1.xaml.cs
using System.Windows;
namespace MyProject.Views
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1: WindowBase
{
public Window1()
{
InitializeComponent();
}
}
}
I am overall following the MVVM pattern, and for the most part from all the articles and videos I have looked at online, they all follow this basic approach and they say it all works, but I can't seem to get it to work.
An additional note is that whenever I add my custom window control to the Window1.xaml file, it breaks the designer and says it is "Invalid Markup"
Also note I added my "WindowBaseStyle" resource dictionary to the App.xaml file as a merged resource dictionary.
Any help is greatly appreciated!! Thanks
Ok, as we discussed in the comments, it seems like the fastest solution to the problem you described is to use a StaticResource to get the window's style from a resource dictionary (or create an implicit style for windows.) I questioned the role of CustomWindow because I thought that might be causing problems with your default style override. (Remember: if you go the lookless control route and try to use the DefaultStyleKeyProperty override, you have to do this on every subclass of that control.)
However, I think doing something like this will get you reusable plumbing for multiple windows driven by viewmodels...
PopupHost
A class that would derive from your customized window. This code provides the following behaviors:
Allows the viewmodel to mark itself as having served its purpose, causing the window to close.
Allows attached properties to be specified by individual views that can affect the way the window appears on-screen, e.g. the window title.
Can be extended to notify presented items that the user has tried closing the window, allowing interception/cancellation or cleanup actions to be performed.
Code:
public class PopupHost : Window
{
private readonly AwaitableViewModelBase _viewModel;
public PopupHost(Window owner, AwaitableViewModelBase viewModel, string dataTemplateKey = null)
{
Owner = owner;
_viewModel = viewModel;
// Wrap the content in another presenter -- makes it a little easier to get to in order to look for attached properties.
var contentPresenter = new ContentPresenter
{
Content = viewModel
};
if (!string.IsNullOrWhiteSpace(dataTemplateKey))
contentPresenter.ContentTemplate = (DataTemplate) FindResource(dataTemplateKey);
Content = contentPresenter;
Task.Run(async () =>
{
await viewModel.Task;
Dispatcher.Invoke(Close);
});
Closed += ClosedHandler;
ApplyTemplate();
// Grab attached property values from the user control (or whatever element... you just need to find the descendant)
var contentElement = FindDescendantWithNonDefaultPropertyValue(contentPresenter, PopupWindowProperties.TitleProperty);
if (contentElement != null)
{
var binding = new Binding { Source = contentElement, Path = new PropertyPath(PopupWindowProperties.TitleProperty) };
SetBinding(TitleProperty, binding);
}
}
private void ClosedHandler(object sender, EventArgs args)
{
_viewModel?.Cancel();
Closed -= ClosedHandler;
}
private static Visual FindDescendant(Visual element, Predicate<Visual> predicate)
{
if (element == null)
return null;
if (predicate(element))
return element;
Visual foundElement = null;
(element as FrameworkElement)?.ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = FindDescendant(visual, predicate);
if (foundElement != null)
break;
}
return foundElement;
}
private static Visual FindDescendantWithNonDefaultPropertyValue(Visual element, DependencyProperty dp)
{
return FindDescendant(element, e => !(dp.GetMetadata(e).DefaultValue ?? new object()).Equals(e.GetValue(dp)));
}
}
PopupWindowProperties
Just a dumb object containing solely attached properties so your views can convey some information to the window.
public static class PopupWindowProperties
{
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(PopupWindowProperties), new FrameworkPropertyMetadata(string.Empty));
public static void SetTitle(UIElement element, string value) => element.SetValue(TitleProperty, value);
public static string GetTitle(UIElement element) => element.GetValue(TitleProperty) as string;
}
AwaitableViewModelBase
A simple abstract viewmodel which has a TaskCompletionSource. This allows the popup window and the viewmodel to coordinate closing.
public abstract class AwaitableViewModelBase : ViewModelBase
{
protected TaskCompletionSource<bool> TaskCompletionSource { get; set; }
public Task<bool> Task => TaskCompletionSource?.Task;
public void RegisterTaskCompletionSource(TaskCompletionSource<bool> tcs)
{
var current = TaskCompletionSource;
if (current != null && current.Task.Status == TaskStatus.Running)
throw new InvalidOperationException();
TaskCompletionSource = tcs;
}
public virtual void Cancel() => SetResult(false);
protected void SetResult(bool result) => TaskCompletionSource?.TrySetResult(result);
}
WindowService
Last but not least, the simple service that can present the requested view and viewmodel. You can use implicit DataTemplates for your viewmodels, or provide the specific x:Key value of the template you wish to use. Note that the await doesn't really do anything here because ShowDialog blocks. We return the bool since it can be used to easily identify if the user hit OK or Cancel on a modal.
public class WindowService
{
public async Task<bool> ShowModalAsync(AwaitableViewModelBase viewModel, string dataTemplateKey = null)
{
var tcs = new TaskCompletionSource<bool>();
viewModel.RegisterTaskCompletionSource(tcs);
Application.Current.Dispatcher.Invoke(() =>
{
var currentWindow = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive) ?? Application.Current.MainWindow;
var window = new PopupHost(currentWindow, viewModel, dataTemplateKey);
window.ShowDialog();
});
return await viewModel.Task;
}
}

In WPF Textblock with multiple runs how do i apply a style to a particular one?

How do I apply a style not on the whole textblock, but only on the first run (the Bold)?
I want to apply the style "XXXFontName-Bold" on the Bold Run AND the style "XXXFontName-Thin" on the rest.
// add button
Button btn = new Button();
TextBlock contextText = new TextBlock();
contextText.Inlines.Add(new Bold(new Run(label.Substring(0,1))));
contextText.Inlines.Add(new Style()); <===== OBVIOUS ERROR HERE
contextText.Inlines.Add(label.Substring(1));
contextText.FontSize = 25;
contextText.Style = FindResource("XXXFontName-Thin") as Style;
btn.Content = contextText;
3 Example Styles, Example to set runs in XAML with the styles and new lines and also how to set them in the code behind in your button
Your code behind:
public MainWindow()
{
InitializeComponent();
Button btn = new Button();
TextBlock contextText = new TextBlock();
var newRun = new Run("BoldGreenRunStyle");
newRun.Style = FindResource("BoldGreenRunStyle") as Style;
contextText.Inlines.Add(newRun);
newRun = new Run("ItalicRedRunStyle");
newRun.Style = FindResource("ItalicRedRunStyle") as Style;
contextText.Inlines.Add(newRun);
newRun = new Run("ThinPurpleRunStyle");
newRun.Style = FindResource("ThinPurpleRunStyle") as Style;
contextText.Inlines.Add(newRun);
btn.Content = contextText;
Container.Children.Add(btn);
}
Your XAML
<Window.Resources>
<Style TargetType="Run" x:Key="BoldGreenRunStyle">
<Setter Property="Foreground" Value="Green"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
<Style TargetType="Run" x:Key="ItalicRedRunStyle">
<Setter Property="Foreground" Value="Red"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
<Style TargetType="Run" x:Key="ThinPurpleRunStyle">
<Setter Property="Foreground" Value="Purple"></Setter>
<Setter Property="FontWeight" Value="Thin"></Setter>
</Style>
</Window.Resources>
<StackPanel x:Name="Container">
<Label Content="From XAML"></Label>
<TextBlock>
<TextBlock.Inlines>
<Run Style="{StaticResource BoldGreenRunStyle}">BoldGreenRunStyle</Run>
<LineBreak/>
<Run Style="{StaticResource ItalicRedRunStyle}">ItalicRedRunStyle</Run>
<LineBreak/>
<Run Style="{StaticResource ThinPurpleRunStyle}">ThinPurpleRunStyle</Run>
<LineBreak/>
</TextBlock.Inlines>
</TextBlock>
</StackPanel>

Show contents as per the selected tabitem in WPF

I have an application named App Window. In which i need to display the installed apps in the tab items. Each tab item contains the data about its corresponding application. If a new app installed, then window will create a new tab item for the newly installed app.
*I have done upto creating a new tab item as per the installed applications.
Code i used:
private List<TabItem> _tabItems;
private TabItem _tabAdd;
try
{
_tabItems = new List<TabItem>();
InstalledApps.DataContext = _tabItems;
InstalledApps.SelectedIndex = 0;
if (A Installed)
this.AddTabItem("A");
if (B Installed)
this.AddTabItem("B");
}
catch (Exception ex)
{
}
private TabItem AddTabItem(string AppName)
{
int count = 1;
// create new tab item
TabItem tab = new TabItem();
tab.Header = AppName;
string tabName = AppName.ToLower();
tab.Name = tabName;
tab.HeaderTemplate = InstalledApps.FindResource("TabHeader") as DataTemplate;
TextBox txt = new TextBox();
txt.IsReadOnly = true;
tab.Content = txt;
_tabItems.Insert(count - 1, tab);
return tab;
}
Xaml:
<TabControl Height="555" HorizontalAlignment="Left" Margin="0,-27,0,0" Name=" InstalledApps " ItemsSource="{Binding}" BorderThickness="0,0,0,0" SelectionChanged=" InstalledApps_SelectionChanged" VerticalAlignment="Top" Width="992" Grid.ColumnSpan="3" Grid.RowSpan="4">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Name="Panel">
<ContentPresenter x:Name="ContentSite" VerticalAlignment="Center"
HorizontalAlignment="Center" ContentSource="Header" Margin="80,10" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="#FFEFEFEF" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="#FFE0E0E0" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="TabHeader" DataType="TabItem">
<DockPanel>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem }, Path=Header}" />
</DockPanel>
</DataTemplate>
</TabControl.Resources>
</TabControl>
My requirement is:
While i am selecting anyone tab from the listed tab items, then it should show its corresponding contents(logo, app name) in is content part.
Thanks in advance,
Kathiresan S.
Create class representing the data you want to display. e.g:
public class AppDetails {
public Uri Logo { get; set; }
public string AppName { get; set; }
//etc
}
then set the AppDetails to TabItem.Content and define TabItem.ContentTemlate similarly as you did with HeaderTemplate.
tab.ContentTemplate = InstalledApps.FindResource("TabContent") as DataTemplate;
tab.Content = new AppDetails{ ... };
alternatively, you can skip ContentTemplate and assign some FrameworkElement (e.g. custom UserControl) directly to the tab content:
tab.Content = new AppDetailsUserControl(AppName, Logo);
Ans to If a new app installed, then window will create a new tab item for the newly installed app.
you should bind the DataContext of TabControl to ObservableCollection so that any new Tabitem added by backend will be shown UI directly.
Ans to While i am selecting anyone tab from the listed tab items, then it should show its corresponding contents(logo, app name) in is content part
You should set the content tab.Content to a UserControl or Model (if Model then define DataTemplate in ResourceDictionary). UserControl or DataTemplate can define what information should be displayed at what control and their visual hierarchy.

wpf - add setter from code

I try to add button from code and add triggers and setters to it. I wolud like to create button like this:
<Button Height="25" Width="100" Name="TestColorButton" Margin="10, 5, 0, 0">
<TextBlock Text="{Binding Text, ElementName=ColorTextBox}"></TextBlock>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding Fill, ElementName=NormalRectangle}"/>
<Setter Property="Template"> <!-- I need this setter -->
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding Fill, ElementName=MouseOverRectangle}"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{Binding Fill, ElementName=ClickRectangle}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
I found how to do everything except one setter. I checked it by comment. I tried many times and I got only something like this:
ContentPresenter contentPresenter = new ContentPresenter
{
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Border border = new Border();
var binding = new Binding("Background");
binding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
BindingOperations.SetBinding(border, BackgroundProperty, binding);
border.Child = contentPresenter;
ControlTemplate controlTemplate = new ControlTemplate(typeof (Button));
Setter templateSetter = new Setter(TemplateProperty, controlTemplate);
style.Setters.Add(templateSetter);
I know it can not work, but I don't know how to do it differently.
Template can be added by code easily. here an example with a label.
// create the label with some context
var lbl = new Label() { DataContext = new ImageData(imagePath) };
// control template i want to put on the label
ControlTemplate labelLayout = new ControlTemplate();
// will be my main container that will be by template later on
FrameworkElementFactory grdContainer = new FrameworkElementFactory(typeof(Grid));
grdContainer.Name = "myContainer";
// another element but ill put it in the grid (template)
FrameworkElementFactory myImage = new FrameworkElementFactory(typeof(Image));
// some bindings and setters
myImage.SetBinding(Image.SourceProperty, new Binding("ImagePath"));
myImage.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Center);
myImage.SetValue(Image.HeightProperty, height);
myImage.SetValue(Image.WidthProperty, double.NaN);
myImage.SetValue(Image.SnapsToDevicePixelsProperty, true);
// add the image to the grid
grdContainer.AppendChild(myImage);
// set the visual layout of the template to be the grid (main container)
labelLayout.VisualTree = grdContainer;
// set the template (line you are looking for mostly)
lbl.Template = labelLayout;

Categories

Resources