Custom Control Based On Combobox - Replace Part Of The Combobox
-
I'm looking at the deault combobox style[^] I'm not entirely sure how to ask what I'm thinking, so please bear with me... Is it possible to replace a part of the control? For example, if I wanted to create a custome combobox. I know in the C# file I just subclass it
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox),
new FrameworkPropertyMetadata(typeof(MyComboBox)));
}// Add some DP's and logic
}
But in the Generic.xaml, I seems like I would need to have ALL the default xaml from the site above - so that it looks & functions like a combobox. Is there a way for me to provide just a new ContentSite? Can I justy define that in my Generic.xaml and somehow apply that to my combobox? Ot do I need to copy in all the default XAML? Thanks
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.
-
I'm looking at the deault combobox style[^] I'm not entirely sure how to ask what I'm thinking, so please bear with me... Is it possible to replace a part of the control? For example, if I wanted to create a custome combobox. I know in the C# file I just subclass it
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox),
new FrameworkPropertyMetadata(typeof(MyComboBox)));
}// Add some DP's and logic
}
But in the Generic.xaml, I seems like I would need to have ALL the default xaml from the site above - so that it looks & functions like a combobox. Is there a way for me to provide just a new ContentSite? Can I justy define that in my Generic.xaml and somehow apply that to my combobox? Ot do I need to copy in all the default XAML? Thanks
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.
I seriously recommend picking up a book on WPF. This kind of stuff is covered in them. Yes, you can replace just about anything you want in a WPF control. You just have to provide the implementation of what you want to replace, not the entire control or any of the implementation above what you're replacing. The trick is knowing what in the XAML tree you need to replace, and where in the tree you need to provide it. Say you want to replace the Text property of a TextBox with a set of async priority bindings. You don't need to provide all the other XAML for a TextBox. You just need to specify the bindings:
There isn't a XAML and C# component to a control. XAML is run through a compiler to generate the C# code. They are interchangeable, not complimentary to each other. The C# code for the control can exist entirely without XAML and the control will still work just fine. XAML is just a structured representation of a bunch of C# classes with properties being set. Can you replace the ContentControl of a ComboBox? Sure. Can you replace the XAML and provide your own render code for the Combo? Again, sure. Is it appropriate to do this? I have no idea what you're trying to do with the Combo, so I can't say. It's also possible to build your own control that looks and acts like a Combo, but doesn't use a ComboBox, or is even based on the existing ComboBox, control at all. You can build one using a TextBox and a PopUp control to do weird stuff like a "ComboBox" that drops down and shows a bunch of TreeView controls in it.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
-
I seriously recommend picking up a book on WPF. This kind of stuff is covered in them. Yes, you can replace just about anything you want in a WPF control. You just have to provide the implementation of what you want to replace, not the entire control or any of the implementation above what you're replacing. The trick is knowing what in the XAML tree you need to replace, and where in the tree you need to provide it. Say you want to replace the Text property of a TextBox with a set of async priority bindings. You don't need to provide all the other XAML for a TextBox. You just need to specify the bindings:
There isn't a XAML and C# component to a control. XAML is run through a compiler to generate the C# code. They are interchangeable, not complimentary to each other. The C# code for the control can exist entirely without XAML and the control will still work just fine. XAML is just a structured representation of a bunch of C# classes with properties being set. Can you replace the ContentControl of a ComboBox? Sure. Can you replace the XAML and provide your own render code for the Combo? Again, sure. Is it appropriate to do this? I have no idea what you're trying to do with the Combo, so I can't say. It's also possible to build your own control that looks and acts like a Combo, but doesn't use a ComboBox, or is even based on the existing ComboBox, control at all. You can build one using a TextBox and a PopUp control to do weird stuff like a "ComboBox" that drops down and shows a bunch of TreeView controls in it.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles. Dave Kreskowiak
I do WPF for a living. I have the Control Development book. In the part about creating custom controls, ALL the xaml from the template is used in the examples. I want to create subclass of a combox box. I don't need the XAML for the button or the textbox, or any other part except for the content site. If you go look at the XAML in the link I provided, I want to replace this:
Something like this
public class MyComboBox : ComboBox
{
static MyComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyComboBox),
new FrameworkPropertyMetadata(typeof(MyComboBox)));
}// Add DP's \* logic here
}
and
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ComboBox"> </ControlTemplate> </Setter.Value> </Setter>
If I do this XAML, the ENTIRE template is replaced, so I'd have to recreate the textbox, button, etc. What I'm asking is, instead of replacing the ENTIRE TEMPLATE, can I somehow replace just a small piece of that template?
In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.