Alternative to "DropDownWidth" Property for Combo Box in wpf - wpf

I am unable to locate a property similar to WindowsForm "DropDownWidth" Property for the Combo Box in WPF. Is there a work around to achieve this functionality?

I don't remember if there is such property in a combobox, but you always can alter a default control template. In your case you should specify a width property of a popup element in a control template. Here is a sample code, taken from one of the WPF themes from Codeplex:
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="{x:Type ComboBox}">
...
<Popup ... Width="100" >
...
</ControlTemplate>
This is a general idea. You can look in a themes source code fore more information. This MSDN pages can also be helpful:
Customizing the Appearance of an Existing Control by Creating a ControlTemplate
ComboBox Styles and Templates
Control Styles and Templates

Related

How to get (MahApps) metro style in derived control?

I create my own control to look and act like ComboBox.
Approach A: I create UserControl with ComboBox as a content. It HAS the metro style, it looks perfect. It works. However I have to manually recreate ComboBox properties in my control. A lot of redundant, ugly code.
Approach B: I extend ComboBox control itself, so no extra coding is needed. It works as charm, however - it's a ComboBox with a different name, so it's not targeted with the metro style for ComboBox.
How to make my new derived control use the metro style for ComboBox?
Add an implicit style to your App.xaml file (replace CustomComboBox with the name of your derived class):
<Style TargetType="local:CustomComboBox" BasedOn="{StaticResource MetroComboBox}" />
If you are using the Material Design toolkit, it should be:
<Style TargetType="local:CustomComboBox" BasedOn="{StaticResource MaterialDesignComboBox}" />

WPF DataGrid top-left button set/view Content?

Trying to set the Content property of a WPF DataGrid top-left button at run time. I get the button object using the VisualTreeHelper of the DataGrid object and then I successfully set its Content property, as verified using Snoop while running the application. However, the button text is not visible. I suspect this is because there are UI elements on top of the button that use non-transparent background brushes. Upon reading the docs I see a grid that uses storyboards and a rectangle that uses gradient brushes.
Other than editing the WPF DataGrid top-left button style template, what are my options for making the button Content (text) visible?
You could entirely replace the template.
I'd prefer to bind the text of the textblock to a property in a viewmodel personally.
This could get you started.
Put this in scope of the datagrid like in your window resources or a resource dictionary merged in app.xaml.
<Style x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="Red">
<TextBlock Text="X" Foreground="White"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Needs more work, but it shows up on a datagrid sample I have.
( This is something which has dozens of styles from experiments answering questions elsewhere in it ).
(Something funny happens, I am now posting under the same user name as before but SO insists that I must recreate my profile every time I log in. Go figure.)
Thank you Andy for the solution you proposed. For the technical reason above, I am unable to mark this question "Solved".
Now I think a quick way around this is to place a transparent label right on top of the DataGrid button, with IsHitTestVisible = false. I noticed a couple interesting things though:
It seems the button can have a Grid or a string for Content but not both;
The button is already created when the DataGrid Loaded event handler runs, e.g., a Click event handler can be added to it; however, setting the Content to a string at this time doesn't change anything. I guess Content changes when the DataGrid is populated.

Control Template that wraps another control in XAML

I want to create a custom control that extends a built-in control and then has a template that wraps that control with a container?
The C# class:
class ExtraBorderTextBox : TextBox {}
The Xaml:
<ControlTemplate>
<Border>
<TextBox/>
</Border>
</ControlTemplate>
That doesnt' work because the TextBox in the control template isn't my custom control, it is a second instance.
I need access to the properties and events on TextBox, having a different parent doens't make sense, I would have to replicate all of that in my class.
This is a simplified example; imagine Border being replaced with a ContentControl that has a 50 line control template for itself. I guess I want something like ContentPresenter (like I have in the ContentControl), but there isn't anything like a "ControlPresenter". Right? Am I missing something, or am I stuck with replicating my content control for the TextBox, or replicating the TextBox behaviour and presentation for my content control?
Thanks.
Update:
There is an answer here that does what I want, which is to copy the default template for System.Windows.Controls.TextBox. This will do what I want; I can insert my container into that. I was hoping that WPF provided a way that is more maintainable to do something like this, something like a adorner/decorator pattern.
Is there any way to make this better in some way? Would using something like Expression Blend make this so that I don't have to hand-edit the XAML pasted in from the webpage?
You could use the default control template as a base and modify it. The default control templates can be found here: http://msdn.microsoft.com/en-us/library/aa970773.aspx
If I understood you right, you want to inherit from TextBox, do some overriding, and use that new class in XAML.
If so:
1) declare the xmlns namespace at the top of your file:
<UserControl
...
xmlns:local="TheAssemblyWhereExtraBorderTextBoxResides"
...>
2) use your custom textbox:
<ControlTemplate>
<Border>
<local:ExtraBorderTextBox />
</Border>
</ControlTemplate>

Custom content control default content

I have a WPF custom content control inherits from Control and when developer drags this control from toolbox i want it to have a default content like expander control has.
<Expander>
<Grid />
</Expander>
How can i do that ?
Thanks
Instead of inheriting from Control, you'll need to inherit from ContentControl.
Here's a sample blog post showing how this can be done. (The post is using Silverlight, but the technique is identical for WPF.)

how to reference current control in grid?

I want to creat my own control:
public class DataGrid : System.Windows.Controls.DataGrid
In the style definition, I want to add a button above the grid, so I wrote:
<Style TargetType="local:DataGrid">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DataGrid">
<Grid>
<Button Content="Addnew"></Button>
<?????>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But how can I tell the xaml to put the grid at postion ????? ?
Thank you all!!
Are you sure that you want to use inheritance here? You should consider creating another control that contains a DataGrid rather than inheriting from DataGrid and use the default Template.
If you decide that you do need to customize the Template of the DataGrid you will need to recreate the entire DataGrid template. You can find the original DataGrid template by opening the DataGrid's assembly in .net reflector or a similar application and opening the embedded resource "generic.xaml". This file will contain a ResourceDictionary defining all the default styles for the Controls defined in the assembly. You can copy the default Template from here and modify it as necessary.
Alternatively, if you have Expression Blend you can have it do this automatically by right clicking on the DataGrid control and choosing "edit a copy of this template" (or something like that, I can't remember the exact wording off the top of my head).

Resources