Is there a way to redefine/alias an existing SolidColorBrush (or any other resource, actually)?
Case in point: I have a brush in an external ResourceDictionary that I want to reference by my own key instead of the original key. I don't want to be dependent on the external reference, since the actual brush is prone to change in the future.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="SomeExternalResource">Red</SolidColorBrush>
</Window.Resources>
<Grid>
<Grid.Resources>
<StaticResourceExtension ResourceKey="SomeExternalResource" x:Key="SomeAliasedResource"/>
</Grid.Resources>
<Border Background="{StaticResource SomeAliasedResource}"/>
</Grid>
</Window>
I don't want to be dependent on the
external reference, since the actual
brush is prone to change in the
future.
You'll still be dependent on the external resource, just not in as many places.
I have an update to Ruedi's solution. This works for resources both within the same resource dictionary and anywhere within the application.
public class Alias : MarkupExtension
{
public string ResourceKey { get; set; }
public Alias()
{
}
public Alias(string resourceKey)
{
ResourceKey = resourceKey;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return _ProvideLocalValue(serviceProvider) ?? _ProvideApplicationValue();
}
private object _ProvideLocalValue(IServiceProvider serviceProvider)
{
var rootObjectProvider = (IRootObjectProvider)
serviceProvider.GetService(typeof(IRootObjectProvider));
if (rootObjectProvider == null) return null;
var dictionary = rootObjectProvider.RootObject as IDictionary;
if (dictionary == null) return null;
return dictionary.Contains(ResourceKey) ? dictionary[ResourceKey] : null;
}
private object _ProvideApplicationValue()
{
var value = Application.Current.TryFindResource(ResourceKey);
return value;
}
}
The usage is similar to above, but the key is to use Alias as the markup extension where used, not StaticResource. Somewhere in the application resource stack, define the resources...
<Application x:Class="WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="clr-namespace:WPF"
StartupUri="MainWindow.xaml">
<Application.Resources>
<system:String x:Key="Text">Display some text.</system:String>
<system:String x:Key="Other">4</system:String>
<wpf:Alias x:Key="Alias" ResourceKey="Text"/>
<wpf:Alias x:Key="Second" ResourceKey="Other"/>
</Application.Resources>
</Application>
And wherever you're referencing the aliases...
<Window x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpf="clr-namespace:WPF"
Title="MainWindow" Height="150" Width="300">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="{wpf:Alias Alias}"/>
<TextBlock Text="{wpf:Alias Second}"/>
</StackPanel>
</Window>
My solution required referencing strings, but it works for any object you want to alias.
You can try to tuse the StaticResourceExtension, but in global resource dictionaries this does not work (strange compiler and runtime errors):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<SolidColorBrush x:Key="StatusColor_OK" Color="#ff32a248" />
<StaticResourceExtension
x:Key="AliasKey"
ResourceKey="StatusColor_Error" />
</ResourceDictionary>
To overcome this problem, I created the following class:
/// <summary>
/// Defines an Alias for an existing resource. Very similar to
/// <see cref="StaticResourceExtension"/>, but it works in
/// ResourceDictionaries
/// </summary>
public class Alias: System.Windows.Markup.MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
IRootObjectProvider rootObjectProvider = (IRootObjectProvider)
serviceProvider.GetService(typeof (IRootObjectProvider));
if (rootObjectProvider == null) return null;
IDictionary dictionary = rootObjectProvider.RootObject as IDictionary;
if (dictionary == null) return null;
return dictionary[ResourceKey];
}
public object ResourceKey { get; set; }
}
Usage:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<SolidColorBrush x:Key="StatusColor_OK" Color="#ff32a248" />
<Alias
x:Key="AliasKey"
ResourceKey="StatusColor_Error" />
</ResourceDictionary>
Related
I created a custom checkbox using a UserControl with Image and Label inside. I want to swap the Checked and Unchecked images whenever i click it.
So far i tried doing the following
<Image Source="{Binding StateImage}"/>
I have a property named StateImage
public String StateImage
{
get
{
return is_checked?"{StaticResource Checked}":"StaticResource Unchecked";
}
}
My code doesn't work and i ended up doing like this:
public String StateImage
{
get
{
return is_checked?"/Resources/Images/Checked.png":"/Resources/Images/Unchecked.png";
}
}
the is_checked variable is modified under MouseDown Event of the UserControl
Is there an easier way I can call the image without writing the whole path and filename?
You could define the resources as strings in the UserControl:
<UserControl x:Class="WpfApplication1.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
<s:String x:Key="Checked">pic.png</s:String>
<s:String x:Key="UnChecked"></s:String>
</UserControl.Resources>
<Grid Background="Yellow">
<Image Source="{Binding StateImage}"/>
</Grid>
</UserControl>
private bool is_checked;
public String StateImage
{
get
{
return is_checked ? Resources["Checked"] as string : Resources["UnChecked"] as string;
}
}
I'm trying to implement a progressbar in my WPF application.
So I added one to my view
<ProgressBar Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Height="31"
Minimum="0"
Maximum="50"
Value="{Binding CurrentProgress}" />
My ViewModel got a new property:
public int CurrentProgress
{
get { return mCurrentProgress; }
set
{
if (mCurrentProgress != value)
{
mCurrentProgress = value;
RaisePropertyChanged("CurrentProgress");
}
}
}
When my load command executes, it raises an Generated event for every file loaded.
And the EventHandler for this event adds +1 to the 'CurrentProgress' property like this:
private void GeneratedHandler(object sender, EventArgs eventArgs)
{
CurrentProgress++;
}
But I don't see any progress on the bar. Does anybody see what I'm doing wrong?
Thanks in advance!
I've tried reproducing your problem, but it worked just fine here.
Anyway, there are a few steps that you can follow:
Make sure that you are not loading your files on the UI thread. If you are, take a look at "Showing progress while performing a lengthy task" on this article.
Make sure the DataContext of your Window is correct, your ViewModel implements System.ComponentModel.INotifyPropertyChanged and your RaisePropertyChanged method is correct.
Here's the code I've used (do not copy and paste the app.xml):
ViewModel:
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string property = "")
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
private int _Progress;
public int Progress
{
get
{
return _Progress;
}
set
{
if(value != Progress)
{
_Progress = value;
NotifyPropertyChanged();
}
}
}
}
MainWindow.xml
<Window x:Class="WpfApplication1.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"
DataContext="{StaticResource ResourceKey=ViewModel_MainWindow}">
<Grid>
<ProgressBar Value="{Binding Progress}" Minimum="0" Maximum="50" />
</Grid>
And the app.xaml:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
xmlns:local="clr-namespace:WpfApplication1" > <!--change the namespace to the one where you ViewModel is-->
<Application.Resources>
<local:MainWindowViewModel x:Key="ViewModel_MainWindow" /> <!--important-->
</Application.Resources>
I have a MainWindow containing a UserControl, both implemented in MVVM-pattern.
The MainWindowVM has properties that I want to bind to properties in the UserControl1VM. But this doesn't work.
Here's some code (the viewmodels use some kind of mvvm-framework that implement the INotifyPropertyChanged in a ViewModelBase-class but that's hopefully no problem):
MainWindow.xaml:
<Window x:Class="DPandMVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DPandMVVM"
Title="MainWindow" Height="300" Width="300">
<Grid>
<local:UserControl1 TextInControl="{Binding Text}" />
</Grid>
</Window>
CodeBehind MainWindow.xaml.cs:
using System.Windows;
namespace DPandMVVM
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowVM();
}
}
}
MainWindow-ViewModel MainWindowVM.cs:
namespace DPandMVVM
{
public class MainWindowVM : ViewModelBase
{
private string _text;
public string Text { get { return _text; } }
public MainWindowVM()
{
_text = "Text from MainWindowVM";
}
}
}
And here the UserControl1.xaml:
<UserControl x:Class="DPandMVVM.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="{Binding TextInTextBlock}" />
</Grid>
</UserControl>
The Codebehind UserControl1.xaml.cs:
using System.Windows.Controls;
namespace DPandMVVM
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
DataContext = new UserControl1VM();
}
}
}
And the Viewmodel UserControl1VM.cs:
using System.Windows;
namespace DPandMVVM
{
public class UserControl1VM : DependencyObject
{
public UserControl1VM()
{
TextInControl = "TextfromUserControl1VM";
}
public string TextInControl
{
get { return (string)GetValue(TextInControlProperty); }
set { SetValue(TextInControlProperty, value); }
}
public static readonly DependencyProperty TextInControlProperty =
DependencyProperty.Register("TextInControl", typeof(string), typeof(UserControl1VM));
}
}
With this constellation the DP cannot be found in MainWindow.xaml.
What am I doing wrong?
First of all you want DependencyProperty TextInControl to be declared inside UserControl1 if you want to bind it from outside.
Move the declaration of DP inside of UserControl1.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string TextInControl
{
get { return (string)GetValue(TextInControlProperty); }
set { SetValue(TextInControlProperty, value); }
}
public static readonly DependencyProperty TextInControlProperty =
DependencyProperty.Register("TextInControl", typeof(string),
typeof(UserControl1));
}
Second you have externally set DataContext of UserControl to UserControl1VM,
public UserControl1()
{
InitializeComponent();
DataContext = new UserControl1VM(); <-- HERE (Remove this)
}
So WPF binding engine looking for property Text in UserControl1VM instead of MainWindowVM. Remove setting DataContext and update XAML of UserControl1 to this:
<UserControl x:Class="DPandMVVM.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="userControl1">
<Grid>
<TextBlock Text="{Binding TextInTextBlock, ElementName=userControl1}" />
</Grid>
</UserControl>
Bind DP using ElementName by setting x:Name on UserControl.
UPDATE
In case you want to have ViewModel intact for UserControl, you have to update binding in MainWindow. Explicitly tell WPF binding engine to look for property in MainWindow's DataContext using ElementName in binding like this:
<local:UserControl1 TextInControl="{Binding DataContext.Text,
ElementName=mainWindow}" />
For this you need to set x:Name="mainWindow" on window root level.
The XAML of your control right now reference the property TextInTextBlock via the DataContext which in turn "Points" to your main window's view model. Reference the data of the control and you are done (btw do not set the DataContext for that reason - the binding won't work any more):
<UserControl x:Class="DPandMVVM.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="self">
<Grid>
<TextBlock Text="{Binding TextInTextBlock, ElementName=self}" />
</Grid>
</UserControl>
This is how I do UserControls with MVVM and DP binding. It's similar to Rohit's answer but with some slight changes. Basically you need to set the Control's internal view model to be the DataContext of the root container within the UserControl rather than the UserControl itself, that way it will not interfere with DP bindings.
E.g.
UserControl XAML
<UserControl x:Class="DPandMVVM.UserControl1"
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"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="userControl1">
<Grid x:Name="Root">
<TextBlock Text="{Binding TextFromVM}" />
</Grid>
UserControl Code-behind
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.ViewModel = new UserControlVM();
}
public UserControlVM ViewModel
{
get { return this.Root.DataContext as UserControlVM ; }
set { this.Root.DataContext = value; }
}
public string TextFromBinding
{
get { return (string)GetValue(TextFromBindingProperty); }
set { SetValue(TextFromBindingProperty, value); }
}
public static readonly DependencyProperty TextFromBindingProperty =
DependencyProperty.Register("TextFromBinding", typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata(null, OnTextBindingChanged));
private static void OnTextBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uc = d as UserControl1;
uc.ViewModel.TextFromVM = e.NewValue as string;
}
}
This means that the control derives it's values from the Root element DataContext which is our ViewModel but the ViewModel can be updated via a DP binding from outside the control (in your case a binding to the parent Window's ViewModel, see below)
Window XAML
<Window x:Class="DPandMVVM.Window1"
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:local="clr-namespace:DPandMVVM"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="window1">
<Grid x:Name="Root">
<local:userControl1 TextFromBinding="{Binding TextFromWindowVM}" />
</Grid>
I have a method that I believe is a lot simpler, and probably more true to MVVM.
In the main window XAML:
<myNameSpace:myUserControl DataContext="{Binding Status}"/>
In your main view model (the data context of the main window:
public myUserControlViewModel Status { set; get; }
now you can in the constructor (or whenever you want to instantiate it):
Status = new myUserControlViewModel();
then if you want to set the text property:
Status.Text = "foo";
and make sure you have the binding setup to a property named Text inside your myUserControlViewModel class:
<TextBox Text="{Binding Text}"/>
and make sure the property fires PropertyChanged, of-course.
Plus, if you use Resharper. You can create a Design instance of the UserControl in your XAML so that it can link the bindings and not tell you that the property is never used by doing this:
<UserControl x:Class="myNameSpace.myUserControl"
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:myNameSpace="clr-namespace:myNameSpace"
d:DataContext="{d:DesignInstance myNameSpace.myUserControl}"
mc:Ignorable="d" ...>
This part:
xmlns:myNameSpace="clr-namespace:myNameSpace"
d:DataContext="{d:DesignInstance myNameSpace.myUserControl}"
Here's a possible working solution for you. However, I've noted in a comment above that this will work in code and perhaps (like my situation) will show up as an error (Object Not Found) in the designer:
<local:UserControl1 TextInControl="{Binding DataContext.Text,
Source={x:Reference <<Your control that contains the DataContext here>>}}" />
I'd rather to have a cleaner solution, though, without any designer errors. I wish to find out how to properly bind a dependency property in a user control to a value coming from the window it's contained in. What I'm finding is that whatever I try to do (short of what I showed above), such as using ElementName and/or AncestorType/Level, etc., the debugger complains that it can't find the source and shows that it's looking for the source inside the context of the user control! It's like I can't break out of the user control context when doing Binding logic in the use of that control (other than that "designer-breaking" solution above).
UPDATE:
I noticed that this might not work for you as your situation might force a problem I just noticed if I change my own source to reference the window instead of a control that has the data context. If I reference the window then I end up with a cyclical redundancy. Perhaps you'll figure out a way to use the Source version of the binding that will work okay for you.
I must also add that my situation is probably a bit more complex since my usercontrol is used in the context of a popup.
I have a static collection of strings and would like to display one of these strings from a certain index. That index is supplied at run time, and is an integer value in the second index in another collection.
I can bind to the static collection, but how do I bind the path to the value in the other
collection?
That is, what do I use as a value to the Path argument in the TextBlock binding?
The code here is just for experimenting and is not part of the working code. It is Visual Studio Designer friendly, and if I get the binding right, it will show Wednesday in the designer without running:
<Window x:Class="BindingCollectionIndex.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindingCollectionIndex"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:AnotherClass
x:Key="Foo" />
</Window.Resources>
<Grid>
<TextBlock
Text="{Binding Source={x:Static local:MyStaticCollections.Days},
Path=[**Wrong ... Foo.CollectionOfIntegers[2]**]}" />
</Grid>
The static class:
using System.Collections.Generic;
namespace BindingCollectionIndex
{
static class MyStaticCollections
{
public static List<string> Days =new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
}
}
The class that supplies the index value:
using System.Collections.ObjectModel;
namespace BindingCollectionIndex
{
class AnotherClass
{
private ObservableCollection<int> collectionOfIntegers = new ObservableCollection<int>
{
1,2,3,4,5
};
public ObservableCollection<int> CollectionOfIntegers
{
get
{
return collectionOfIntegers;
}
}
}
}
There is nothing added to the xaml's code behind.
Thanks for reading.
David
Have you considered using a Dictionary?
class DaysViewModel
{
public IDictionary<int,string> Days { get; set; }
public DaysViewModel()
{
Days = new Dictionary<int, string>
{
{1,"Monday"},
{2,"Tuesday"},
{3,"Wednesday"},
{4,"Thursday"},
{5,"Friday"}
};
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DaysViewModel x:Key="Week" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding Source={StaticResource Week},Path=Days[2]}" />
</Grid>
Hey Guys (and girls if any :)
I needed a different list box selection policy than the one provided by default with WPF listboxes, i.e. beeing able to have an extended selection withou any modifier key.
No problem on that, here is what I did :
class EnhancedCcyPairListBox : ListBox
{
protected override DependencyObject GetContainerForItemOverride()
{
return new CcyPairListBoxItem();
}
}
internal class CcyPairListBoxItem : ListBoxItem
{
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
IsSelected = !IsSelected;
e.Handled = true;
}
}
I can't tell if it's the best way to do, but it seems to work exactly as I expected.
Except that... by doing so, I have lost the defaults ListBoxItem style I had before. How can I tell to my derived classes to keep their default style ?
Thank you very much !
Add this above the EnhancedCcyPairListBox declaration
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(CcyPairListBoxItem))]
Edit
Add this to the static constructor of CcyPairListBoxItem"
DefaultStyleKeyProperty.OverrideMetadata(
typeof(CcyPairListBoxItem), new FrameworkPropertyMetadata(typeof(CcyPairListBoxItem)));
and in Themes/Generic.xaml add
<Style TargetType="{x:Type CcyPairListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"/>
Because the WPF context of our application is quite special, I wanted to be sure that it was not an issue that was very specific.
So I took the above two classes and imported them in a brand new WPF test project. I created a Themes directory that registered a Resource dictionnary in which I put the Style as NVM mentionned earlier.
Content of Generic.xaml :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/InheritedListBoxStyling;component/Themes/Styles/TempDic.xaml" />
<!-- Reference here the Resource dictionnary used for your own component -->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Content of TempDic.xaml :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:InheritedListBoxStyling="clr-namespace:InheritedListBoxStyling">
<Style TargetType="{x:Type InheritedListBoxStyling:CcyPairListBoxItem}"
BasedOn="{StaticResource {x:Type ListBoxItem}}"/>
</ResourceDictionary>
Content of Window1.xaml.cs
namespace InheritedListBoxStyling
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = this;
for (int i = 0; i < 50; i++)
{
source.Add("toto " + i);
}
l1.ItemsSource = source;
l2.ItemsSource = source;
}
public ObservableCollection<string> source = new ObservableCollection<string>();
}
}
Content of Window1.xaml :
<Window x:Class="InheritedListBoxStyling.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:InheritedListBoxStyling="clr-namespace:InheritedListBoxStyling"
Title="Window1" Height="300" Width="300">
<Grid>
<ListBox x:Name="l1" SelectionMode="Extended"
Margin="0,0,12,12" Height="100"
HorizontalAlignment="Right"
VerticalAlignment="Bottom" Width="120" />
<InheritedListBoxStyling:EnhancedCcyPairListBox
x:Name="l2" SelectionMode="Extended"
Height="100" Margin="12,12,0,0"
VerticalAlignment="Top"
HorizontalAlignment="Left" Width="120" />
</Grid>
</Window>
And here the result :
=> the default styling is not applied and, as in my "real case" issue, the only kind of styling that seems to be applied is the gray selected item style.
Any idea about what's going on or what I'm doing wrong ?