I have a WPF CustomControl that is derived from ComboBox and I’m trying to figure out how to customize the display of the items. Basically, I want most items to show with normal text but,
depending on the data in each item object, I would like some to display either bold or italic. Normally I do this directly in the XAML, but since it’s a CustomControl I’m a bit at a loss. I would love to be able to just bind it directly in code, but I’m also open to methods that might mean loading in external XAML styles, if you can show me how to do that (I haven’t a clue).
The code below is a basic approximation of the control I’m using but greatly simplified. However, the basic concept of how the data loaded is the same and the data objects themselves are coming from an external source so they can not access the control itself in anyway. The template needs to
just be bound off of the base properties.
public class FormatData
{
public FormatData() { }
public string Name { get; set; }
public bool Bold { get; set; }
public bool Italic { get; set; }
}
public class FormatDropDown : System.Windows.Controls.ComboBox
{
public FormatDropDown()
{
}
public void LoadSelection(FormatData[] data)
{
try
{
this.ItemsSource = data;
this.DisplayMemberPath = "Name";
}
catch (Exception e) { MessageBox.Show(e.Message); ; }
}
}
The control is populated as follows:
var data = new FormatData[]{
new FormatData(){
Name = "Normal"
},
new FormatData(){
Name = "Bold",
Bold = true
},
new FormatData(){
Name = "Italic",
Italic = true
},
new FormatData(){
Name = "BoldItalic",
Bold = true,
Italic = true
},
};
fddTest.LoadSelection(data);
Anyone have an idea of how I can achieve this?
How about this:
And in the theme file (generic.xaml):
So essentially override the DefaultStyleKey for your custom control.