I want to implement this in Silverlight.
Combobox with an inline filter in the popup
http://gregandora.wordpress.com/2012/01/25/filtering-items-in-a-wpf-combobox/
Unfortunately it's for WPF and XAML is not compatible. It's very hard to convert it or understand how to change the control template of the combobox.
Any idea?
Here's a demo of the solution: https://dl.dropbox.com/u/8424800/StackOverflowSl.html (see ComboBox Filter)
I took the default Silverlight ComboBox template and added a "FilterTextBox" to the Popup section. I couldn't post the whole xaml as it exceeded StackOverflow's limit. The full sources are here as a GitHub Gist. I've left in the important parts. Next, the event handlers on the TextBox needs to be hooked up.
<Style TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<Popup x:Name="Popup">
<Border x:Name="PopupBorder"
Height="Auto"
HorizontalAlignment="Stretch"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="3">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#FFFFFFFF" />
<GradientStop Offset="1" Color="#FFFEFEFE" />
</LinearGradientBrush>
</Border.Background>
<Grid>
<TextBox x:Name="FilterTextBox"
Height="22"
VerticalAlignment="Top" />
<ScrollViewer x:Name="ScrollViewer"
Margin="0,25,0,0"
BorderThickness="0"
Padding="1">
<ItemsPresenter />
</ScrollViewer>
</Grid>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Wiring up the TextBox
public Q12513294()
{
// Required to initialize variables
InitializeComponent();
InitializeMyCombo(
Enumerable.Range(1, 99).Select(x => "Beer " + x.ToString() + " on the wall"),
(object item, string filter) => (item as String).Contains(filter)
);
}
private void InitializeMyCombo(IEnumerable items, Func<object, string, bool> filter)
{
MyComboBox.Loaded += (s, e) =>
{
// PagedCollectionView implements a filterable collection
PagedCollectionView list = new PagedCollectionView(items);
MyComboBox.ItemsSource = list;
// Set the filter based on the contents of the textbox
TextBox filterTextBox = MyComboBox.GetTemplateChild<TextBox>("FilterTextBox");
list.Filter = new Predicate<object>(
item => filter(item, filterTextBox.Text)
);
// Refresh the filter each time
filterTextBox.TextChanged += (s2, e2) =>
{
list.Refresh();
filterTextBox.Focus();
};
};
}
public static class Helper
{
public static T GetTemplateChild<T>(this DependencyObject parent, string partName)
{
return (T)(VisualTreeHelper.GetChild(parent, 0) as Panel).FindName(partName);
}
}
Related
I have been working with WPF for some time.
I need to create the following control over Internet, but could not find appropriate.
Can anybody help how to implement this functionality. Value should be increasing or decreasing when clicked on control.
I found that I can use either Volume control or Slider, but not getting clear what I should use.
Thanks in anticipation.
I prefer to use a Progressbar for these kind of displays.
This is my implementation of a simple volume control looking pretty much like the one you show as an example:
public partial class MainWindow : Window, INotifyPropertyChanged
{
private double _volume;
private bool mouseCaptured = false;
public double Volume
{
get { return _volume; }
set
{
_volume = value;
OnPropertyChanged("Volume");
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed && mouseCaptured)
{
var x = e.GetPosition(volumeBar).X;
var ratio = x/volumeBar.ActualWidth;
Volume = ratio*volumeBar.Maximum;
}
}
private void MouseDown(object sender, MouseButtonEventArgs e)
{
mouseCaptured = true;
var x = e.GetPosition(volumeBar).X;
var ratio = x / volumeBar.ActualWidth;
Volume = ratio * volumeBar.Maximum;
}
private void MouseUp(object sender, MouseButtonEventArgs e)
{
mouseCaptured = false;
}
#region Property Changed
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
And the XAML:
<Window x:Class="VolumeControlApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="196" Width="319">
<Window.Resources>
<Style x:Key="VolumeStyle" TargetType="{x:Type ProgressBar}">
<Setter Property="Foreground" Value="#FFB00606"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ProgressBar}">
<Grid x:Name="TemplateRoot">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"/>
<Rectangle x:Name="PART_Track"/>
<Grid x:Name="PART_Indicator" ClipToBounds="True" HorizontalAlignment="Left">
<Rectangle x:Name="Indicator" Fill="{TemplateBinding Foreground}" RadiusX="5" RadiusY="3"/>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid Background="#FF363636">
<Border Background="Gray" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" CornerRadius="3" Padding="2">
<Border Background="Black" CornerRadius="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Vol:" Foreground="White" VerticalAlignment="Center" Margin="4 0"/>
<ProgressBar x:Name="volumeBar" Height="10"
Value="{Binding Volume}"
Width="100"
MouseMove="MouseMove"
MouseDown="MouseDown"
MouseUp="MouseUp" Style="{DynamicResource VolumeStyle}" Grid.Column="1"/>
</Grid>
</Border>
</Border>
</Grid>
</Window>
You could use a slider and create a template for it.
If you need special mouse handling you'll need to subclass the slider and add logic/event handling.
The standard Slider template has a couple of repeat buttons. By simply making the left repeat button red you have a very basic implementation of the required control.
Take a look at this posts hope it helps you..
Link:
1: Sliders
2: Similar to VLC player volume control
So in my main app ResourceDictionary I have a style that starts with
<Style TargetType="{x:Type TabItem}" x:Key="{x:Type TabItem}">
Now what I'm doing is overriding the TabControl to make a custom control(without ANY xaml). The problem is at this point it doesn't inherit the custom TabControl template.
So what I'm wondering, is how can I programatically bind to the 'x:Key' of the template, considering it's bound to a specific control without having my control have a xaml file.
Some places online say to do this
this.Style = (Style)FindResource("TabItem");
But it doesn't seem to work in my situation. The 'style' is in a separate file, and imported into my App.Xaml resource dictionary...so it overrides all TabItems properly, but not the one I overrode.
Here is my App.xaml
<Application x:Class="Octgn.OctgnApp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Themes/Full/ExpressionDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And the item(slightly truncated cause it's large) from the ExpressionDark.xaml
<Style d:IsControlPart="True" TargetType="{x:Type TabItem}" x:Key="{x:Type TabItem}">
<Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="grid" Margin="2,1,2,3">
<Grid.LayoutTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Grid.LayoutTransform>
<Border x:Name="border" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0.5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,0.976" StartPoint="0.5,0.039">
<GradientStop Color="#7F595959" Offset="0" />
<GradientStop Color="#19FFFFFF" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Border x:Name="SelectedBorder" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0" Background="{DynamicResource SelectedBackgroundBrush}"/>
<Border x:Name="HoverBorder" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,0.976" StartPoint="0.5,0.039">
<GradientStop Color="#7F595959" Offset="0" />
<GradientStop Color="#19FFFFFF" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Grid>
<ContentPresenter x:Name="ContentSite" RecognizesAccessKey="True" ContentSource="Header" d:LayoutOverrides="Width, Height" HorizontalAlignment="Center" Margin="6,1,6,1" VerticalAlignment="Center" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This style auto applies to a TabItem, but I'm trying to apply it to my override of TabItem.
Here's the code for that
/// <summary>
/// The chat bar item.
/// </summary>
public class ChatBarItem : TabItem
{
/// <summary>
/// Sets the Chat Room
/// </summary>
private readonly NewChatRoom room;
/// <summary>
/// Initializes static members of the <see cref="ChatBarItem"/> class.
/// </summary>
static ChatBarItem()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(ChatBarItem), new FrameworkPropertyMetadata(typeof(TabItem)));
}
/// <summary>
/// Initializes a new instance of the <see cref="ChatBarItem"/> class.
/// </summary>
/// <param name="chatRoom">
/// The chat Room.
/// </param>
public ChatBarItem(NewChatRoom chatRoom)
{
this.room = chatRoom;
this.ConstructControl();
}
/// <summary>
/// Initializes a new instance of the <see cref="ChatBarItem"/> class.
/// </summary>
public ChatBarItem()
{
this.room = null;
this.ConstructControl();
}
/// <summary>
/// Constructs this control
/// </summary>
private void ConstructControl()
{
//this.Background = Brushes.Transparent;
//this.BorderThickness = new Thickness(0);
//this.Style = (Style)FindResource("TabItem");
// this is where I want to set the style of this control
// Main content object
var mainBorder = new Border { Margin = new Thickness(5) };
// Main content grid
var g = new Grid();
g.ColumnDefinitions.Add(new ColumnDefinition());
g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(16) });
// Create item label
var label = new TextBlock() { VerticalAlignment = VerticalAlignment.Center };
if (this.IsInDesignMode() || this.room == null)
{
label.Inlines.Add(new Run("test"));
}
else
{
label.Inlines.Add(new Run(this.room.GroupUser.User.User));
}
// Create close button
var borderClose = new Border { Width = 16, Height = 16 };
var imageClose = new Image()
{
Source = new BitmapImage(new Uri("pack://application:,,,/Octgn;component/Resources/close.png")),
Stretch = Stretch.Uniform
};
// --Add items to items
// Add close image to closeBorder
borderClose.Child = imageClose;
// Add Close 'button' to grid
g.Children.Add(borderClose);
Grid.SetColumn(borderClose, 1);
// Add label to main grid
g.Children.Add(label);
// Add main grid to main border
mainBorder.Child = g;
// Add main grid to this
this.Header = mainBorder;
}
}
If I had a xaml for the TabItem I could easly just go Style="{DynamicResource {x:Type TabItem}}"(or something similar), but I'm doing it all programatically.
You already tried:
<Style TargetType="{x:Type TabControl}" BasedOn="{StaticResource {x:Type TabControl}}">
</Style>
By the way, you need not define an x:Key, if you want the style to be applied to all TabControl and if you want it to work for your entire application, set the style in App.xaml
Edit: I still do not understand exactly what you want to do. (If you can, edit your question again).
But you can try to set the style manually by:
Style customStyle = (Style)Application.Current.FindResource("StyleKey");
As mentioned,
static MyCustomTabControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomTabControl), new FrameworkPropertyMetadata(typeof(TabControl)));
}
But this will only work if:
1) The style your control is in the file: Themes\Generic.xaml
2) The file "Generic.xaml" has the property "BuildAction" with value = Page.
3) AssemblyInfo.cs contains:
[assembly: ThemeInfo (
ResourceDictionaryLocation.None,
ResourceDictionaryLocation.SourceAssembly
)]
What about this in your static constructor of your dervied tab Control
static MyCustomTabControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomTabControl), new FrameworkPropertyMetadata(typeof(TabControl)));
}
Ok so after some digging around, and the help of the other two answers here, I was able to figure this out. Instead of doing a string for the FindResource, it would seem that the resource dictionary, is in fact a diciontary with an object key. So...
this.Style = (Style)Application.Current.FindResource(typeof(TabItem));
And that did it for me.
Thanks again to the other two answers as well.
I'm trying to build a Chart, with Columns. The columns will have different colors by value.
I'm using MVVM with WPF and Databinding.
How i'm trying to do (Into my ViewModel):
private Int16 colorValue;
public Int16 ColorValue
{
get { return colorValue; }
set
{
colorValue = value;
if (ColorValue < 20)
ColorType = new SolidColorBrush { Color = Colors.Aqua };
if (ColorValue < 40)
ColorType = new SolidColorBrush { Color = Colors.Gray };
if (ColorValue >= 41)
ColorType = new SolidColorBrush { Color = Colors.Black };
}
}
private Brush colorType;
public Brush ColorType
{
get { return colorType; }
set
{
if (value != null)
{
colorType = value;
OnPropertyChanged("ColorType");
}
}
}
Into My Xaml (This is the Static Resource to change the Column Color Attribute):
<Style x:Key="ColorByGradeColumn" TargetType="DVC:ColumnDataPoint">
<Setter Property="Background" Value="DarkGray"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="DVC:ColumnDataPoint">
<Border
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid Background="{Binding ColorType}">
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Color="#77ffffff" Offset="0"/>
<GradientStop Color="#00ffffff" Offset="1"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Border BorderBrush="#ccffffff" BorderThickness="1">
<Border BorderBrush="#77ffffff" BorderThickness="1"/>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My Chart into Xaml:
<Grid Grid.Column="2" Height="368" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Bottom" Width="1009" Grid.ColumnSpan="4" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="198*" />
<ColumnDefinition Width="191*" />
</Grid.ColumnDefinitions>
<DVC:Chart x:Name="ColumnChart"
Grid.ColumnSpan="2">
<DVC:ColumnSeries
AnimationSequence="FirstToLast"
FlowDirection="LeftToRight"
Title="Largura"
ItemsSource="{Binding Path=Placas, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Path=Slab.InfThick}"
DependentValueBinding="{Binding Path=Slab.InfThick}"
IndependentValueBinding="{Binding Path=Slab.SlabId}"
DataPointStyle="{StaticResource ColorByGradeColumn}">
</DVC:ColumnSeries>
</DVC:Chart>
</Grid>
So... My Chart using ColumnSeries get it's attribute by the Static Resource defined into DataPointStyle. StaticResource 'ColorByGradeColumn' i've made a binding to my property ColorType.
Here's the question... Why isn't working? I've followed the steps explained in this link:
Columns of a different color [Customizing the appearance of Silverlight charts with re-templating and MVVM]
And I really don't know what i'm missing.
Thanks in advance.
Maybe I'm misunderstanding, but aren't you covering your grid background colour with a fixed gradient fill?
Ok, we've figured out how to fix that annoying thing:
We've created a converter than will receive the value, and returns the color than we wanted. Before, I was trying to do with a Property:
#region Converters
/// <summary>
/// Retorna a cor do estado da placa
/// </summary>
public class RetornaCorEstadoBarra : DependencyObject, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
var ColorValue = (Int32)value;
if (ColorValue < 800)
return "Aqua";
else if (ColorValue < 1000)
return "Gray";
else //if (ColorValue > 1001)
return "Black";
}
catch
{
return "Black";
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
#endregion
Created a Resource inside xaml file:
<vm:RetornaCorEstadoBarra x:Key="RetornaCorEstadoBarra" />
And created a Style a little below that Resource:
<Style x:Key="ColorByGradeColumn" TargetType="DVC:ColumnDataPoint">
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="{x:Type DVC:ColumnDataPoint}">
<Border
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
Background="{Binding Slab.InfThick,Converter={StaticResource RetornaCorEstadoBarra}}"
>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Ok, now's the catch:
<DVC:Chart x:Name="ColumnChart" Grid.ColumnSpan="2" Width="{Binding Path=GridWidthSize}" >
<DVC:ColumnSeries
AnimationSequence="FirstToLast" FlowDirection="LeftToRight" Title="Largura" ItemsSource="{Binding Path=Placas, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ToolTip="{Binding Path=Slab.SlabId}" DependentValueBinding="{Binding Path=Slab.InfThick}" IndependentValueBinding="{Binding Path=Slab.SlabId}"
DataPointStyle="{StaticResource ColorByGradeColumn}">
<DVC:ColumnSeries.IndependentAxis>
<DVC:CategoryAxis Orientation="X" Visibility="Visible" Foreground="Transparent"/>
</DVC:ColumnSeries.IndependentAxis>
</DVC:ColumnSeries>
</DVC:Chart>
</ScrollViewer>
Well, the problem is, the color into the Chart is a Static Resource. It doesn't change a second time. So, with a 'improvised' dynamic resource, the problem is fixed, right here:
Background="{Binding Slab.InfThick,Converter={StaticResource RetornaCorEstadoBarra}}"
We pass the parameter to that Converter RetornaCorEstadoBarra. It will receive the parameter, and return a color value. Then, inside my Chart, than Binds to my Resource, will populate the Chart with the value received by my Converter. But the Chart only gets the value once. However, my converter will always return a value when he receive a value. That's the catch.
Thanks for the help :)
I want to make a custumized Image Control like MSN with Green light in border
You should use a ContentControl with a custom template. And set the content of the control to the image you want to display.
<ContentControl Template="{DynamicResource TheTemplate}"><Image/></ContentControl>
Then define the style somewhere in your resource dictionary.
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid>
<!-- Add some fancy borders and colors here -->
<ContentPresenter/>
</Grid>
</ControlTemplate>
I would recommend a UserControl, something like this:
<UserControl x:Class="Test.UserControls.BorderedImageControl"
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" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Border CornerRadius="{Binding CornerRadius}" Padding="{Binding BorderThickness}" BorderThickness="1">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE5E5E5" Offset="0"/>
<GradientStop Color="#FF15A315" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FF67FF68" Offset="1"/>
<GradientStop Color="#FF75E476" Offset="0.496"/>
<GradientStop Color="#FF0FC611" Offset="0.509"/>
</LinearGradientBrush>
</Border.Background>
<Border.Child>
<Image Source="{Binding Source}"/>
</Border.Child>
</Border>
</UserControl>
namespace Test.UserControls
{
public partial class BorderedImageControl : UserControl
{
public static readonly DependencyProperty SourceProperty = Image.SourceProperty.AddOwner(typeof(BorderedImageControl));
public ImageSource Source
{
get { return (ImageSource)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty = Border.CornerRadiusProperty.AddOwner(typeof(BorderedImageControl));
public CornerRadius CornerRadius
{
get { return (CornerRadius)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, CornerRadius); }
}
public static readonly DependencyProperty BorderThicknessProperty =
DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(BorderedImageControl), new UIPropertyMetadata(new Thickness()));
public Thickness BorderThickness
{
get { return (Thickness)GetValue(BorderThicknessProperty); }
set { SetValue(BorderThicknessProperty, value); }
}
public BorderedImageControl()
{
InitializeComponent();
}
}
}
This is comparatively simple, if you want those custom curved shapes you probably need to work with paths instead of a border with corner radius.
Usage example:
<uc:BorderedImageControl Source="http://www.gravatar.com/avatar/c35af79e54306caedad37141f13de30c?s=32&d=identicon&r=PG"
CornerRadius="20" BorderThickness="10" MaxWidth="100" Margin="5"/>
Looks like this:
Since Popup doesn't derive from Control and doesn't have a template, how can I define a template so that all popups look the same? I need to design one that has a certain look and don't want to have to copy markup each time one is used.
This seems pretty easy but I can't figure out how to do it. The Child property defines a logical tree but I don't see how you can pull that out into a template and reuse it.
I was looking to do the same thing and here is what I came up with:
I inherited from ContentPresenter, styled that control as I wanted and than placed the derived ContentPresenter inside my Popup, I only used 2 text blocks for the simplicity but it is easy to understand how any content could be added.
My custom control:
using System.Windows;
using System.Windows.Controls;
namespace CustomControls
{
[TemplatePart(Name = PART_PopupHeader, Type = typeof(TextBlock))]
[TemplatePart(Name = PART_PopupContent, Type = typeof(TextBlock))]
public class CustomPopupControl : ContentControl
{
private const string PART_PopupHeader = "PART_PopupHeader";
private const string PART_PopupContent = "PART_PopupContent";
private TextBlock _headerBlock = null;
private TextBlock _contentBlock = null;
static CustomPopupControl()
{
DefaultStyleKeyProperty.OverrideMetadata
(typeof(CustomPopupControl),
new FrameworkPropertyMetadata(typeof(CustomPopupControl)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_headerBlock = GetTemplateChild(PART_PopupHeader) as TextBlock;
_contentBlock = GetTemplateChild(PART_PopupContent) as TextBlock;
}
public static readonly DependencyProperty HeaderTextProperty =
DependencyProperty.Register("HeaderText", typeof(string), typeof(CustomPopupControl), new UIPropertyMetadata(string.Empty));
public string HeaderText
{
get
{
return (string)GetValue(HeaderTextProperty);
}
set
{
SetValue(HeaderTextProperty, value);
}
}
public static readonly DependencyProperty ContentTextProperty =
DependencyProperty.Register("ContentText", typeof(string), typeof(CustomPopupControl), new UIPropertyMetadata(string.Empty));
public string ContentText
{
get
{
return (string)GetValue(ContentTextProperty);
}
set
{
SetValue(ContentTextProperty, value);
}
}
}
}
Style for the control:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControls">
<Style TargetType="{x:Type local:CustomPopupControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomPopupControl}">
<Border CornerRadius="3" BorderThickness="1" BorderBrush="White">
<Border.Background>
<SolidColorBrush Color="#4b4b4b" Opacity="0.75"/>
</Border.Background>
<Border.Effect>
<DropShadowEffect ShadowDepth="0"
Color="White"
Opacity="1"
BlurRadius="5"/>
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="{TemplateBinding HeaderText}"
Grid.Row="0"
Foreground="#5095d6"
FontWeight="Bold"
VerticalAlignment="Bottom"
Margin="{TemplateBinding Margin}"
HorizontalAlignment="Left"/>
<Rectangle Grid.Row="1" Stroke="AntiqueWhite" Margin="1 0"></Rectangle>
<TextBlock Grid.Row="2"
Grid.ColumnSpan="2"
x:Name="PART_TooltipContents"
Margin="5, 2"
Text="{TemplateBinding ContentText}"
TextWrapping="Wrap"
MaxWidth="200"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The use of the control:
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="Button1" Content="Button with popup" HorizontalAlignment="Center">
</Button>
<Button x:Name="Button2" Content="Another button with popup" HorizontalAlignment="Center">
</Button>
<Popup IsOpen="True"
FlowDirection="LeftToRight"
Margin="10"
PlacementTarget="{Binding ElementName=Button1}"
Placement="top"
StaysOpen="True">
<local2:CustomPopupControl HeaderText="Some Header Text" ContentText="Content Text that could be any text needed from a binding or other source" Margin="2">
</local2:CustomPopupControl>
</Popup>
<Popup IsOpen="True"
FlowDirection="LeftToRight"
Margin="10"
PlacementTarget="{Binding ElementName=Button2}"
Placement="Bottom"
StaysOpen="True">
<local2:CustomPopupControl HeaderText="Different header text" ContentText="Some other text" Margin="2">
</local2:CustomPopupControl>
</Popup>
</StackPanel>
I tried illustrating how some properties can be constant across all controls, others can be customized per control and others could be bound to TemplatePart, here is the final result:
Depends how you want your pop-ups to behave. If they're just for displaying information in a uniform manner, than you might want to have a class that derives from Window that has the standard formats and styling wrapped around a ContentPresenter then bind the content of the presenter to a property which can represent the custom information for each pop-up.
Then its just a matter of programatically inserting whatever custom content you want before displaying the pop-up window.
Hope it helps.