DevExpress WPF is driving me mad with things that are very simple.
How do I add margin around a control in DevExpress WPF?
For default WPF control, I can simply add a "Margin" property in control, for example:
<Button Margin="5" />
Done. In DevExpress WPF, there is no Margin property. Take following code as an example - a ComboBox - how do I add Margin to it?
<dxb:BarEditItem x:Name="comboBox" Content="ComboBox" EditWidth="100" EditHeight="35">
<dxb:BarEditItem.EditSettings>
<dxe:ComboBoxEditSettings>
<dxe:ComboBoxEditSettings.Items>
<dxe:ComboBoxEditItem>AAA</dxe:ComboBoxEditItem>
<dxe:ComboBoxEditItem>BBB</dxe:ComboBoxEditItem>
<dxe:ComboBoxEditItem>CCC</dxe:ComboBoxEditItem>
</dxe:ComboBoxEditSettings.Items>
</dxe:ComboBoxEditSettings>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
A bar item is a non-visual object that implements a specific functionality so you can't apply visual properties on it directly.
You can set the editor properties via the BarEditItem.EditStyle property:
<dxb:BarEditItem x:Name="comboBox" Content="ComboBox" EditWidth="100">
<dxb:BarEditItem.EditStyle>
<Style TargetType="dxe:ComboBoxEdit">
<Setter Property="Margin" Value="12,4,12,4"/>
</Style>
</dxb:BarEditItem.EditStyle>
<dxb:BarEditItem.EditSettings>
<dxe:ComboBoxEditSettings>
<dxe:ComboBoxEditSettings.Items>
<dxe:ComboBoxEditItem>AAA</dxe:ComboBoxEditItem>
<dxe:ComboBoxEditItem>BBB</dxe:ComboBoxEditItem>
<dxe:ComboBoxEditItem>CCC</dxe:ComboBoxEditItem>
</dxe:ComboBoxEditSettings.Items>
</dxe:ComboBoxEditSettings>
</dxb:BarEditItem.EditSettings>
</dxb:BarEditItem>
Related
I wonder if someone can help, I am designing a custom WPF window for an application i am working on and I have an issue with the Min, Max and Close buttons. I have designed a ControlTemplate for the 3 buttons and they are in a StackPanel with Vertical orientation. In my base style I have the following
<Style x:Key="BaseWindowButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value={Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
<Setter Property="Background" Value="Transparent" />
...
</Style>
Have also tried setting the Foreground to a specific color such as #FF000000 and nothing displays
I have a style that inherits this style for the buttons but does not change the foreground or background.
My problem is that the button content does not display, the button displays and the IsMouseOver trigger fires which changes the background but the textual content never displays.
The Min button style is
<Button x:Name="PART_Min">
<Path Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
Data="F1M0,6L0,9 9,9 9,6 0,6z"
SnapsToDevicePixels=True" />
</Button>
I am at a loss as to why the content does not display so would appreciate your thoughts.
The Visual Tree is below, I have examined this and identified the Foreground values
Window (Foreground: #FF000000)
Grid
AdornerDecorator
Grid
ContentControl (Foreground: #FF000000)
StackPanel
Button (Foreground: #FF000000)
Grid
ContentControl (Foreground: #FF000000)
But like I said above I have removed the binding and specified a physical value and still do not get the content displaying
Use ContentPresenter instead of ContentControl in your button's template. (You should include the button's control template in a question like this... it's potentially highly relevant.)
As I too was a beginner with WPF, and trying to understand things was a bit of a learning curve, I would like to offer a few previous links posted out here.
First a simple style for creating simple label and having all labels as a default take on this format without explicit style assignments.
Another sample showing creation of a custom button. This is where I went step by step to create a custom class and apply a style to it to show/hide image on a button.
Maybe the button link and style declaration will help you find your button coloring issues too.
My intent is to override phone theme. By default page markup has following code:
<phone:PhoneApplicationPage
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
I replace it with a style reference:
<phone:PhoneApplicationPage
Style="{StaticResource stylePage}"
Style is defined in app resources like this:
<Application.Resources>
<Style x:Key="stylePage" TargetType="phone:PhoneApplicationPage">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="DeepSkyBlue" />
<Setter Property="FontFamily" Value="Arial" />
</Style>
</Application.Resources>
But background is still black, and font is white. Debugger shows that the values were applied.
If I set VisualRoot Grid's background, it's applied, but I want to set fontfamily and foreground values to be used by all my controls by default. Is it possible?
According to this msdn article - only the following properties are inherited in the visual tree in Silverlight: FontFamily, FontSize, FontStretch, FontStyle, FontWeight, and Foreground. Normally - dependency properties are not inherited in Silverlight (they often are in WPF). This is likely why these are not mentioned in the Silverlight MSDN article on dependency property value precedence, but the WPF version of the article mentions inherited value as lower priority than the local value or style setters. Your solution in fact works to some degree, but by default - most controls have font properties set by their style - Buttons by their default style, TextBlocks in page templates use PhoneTextNormalStyle or PhoneTextTitle1Style. If you remove the XML attributes setting the style of your TextBlocks - your application-wide font properties will apply to the TextBlocks.
Background dependency property unfortunately is not inherited, so your background will remain as is.
Your application-wide properties will not work on controls more complex than TextBlocks - like Buttons, since the default style of Buttons and other controls defines the style of TextBlocks explicitly using appropriate system styles.
I would say you should create your own resource dictionaries with styles of your controls and apply these to your controls manually, otherwise - you would need to write some code to automatically replace these properties at run time.
I am currently working on a user control that has white text and a transparent background. Unfortunately because the XAML design view within VS2010 has a white background I cannot see anything that I am designing!
I have been through all the settings dialogs I can think of, but have been unable to find a setting that changes the background colour of the XAML designer.
Does anyone know how this can be done?
Alternatively, as of VS 2013, you can do this in Tools -> Options -> Fonts and Colors, XAML UI Designer.
The editable foreground / background colors there are the colors of the checkerboard background. I just set them both to a darkish grey color that seems to work for both light and dark theme'd background stuff.
In your XAML, set your background to black. Then in your user control, use the DesignerProperties to set the background at runtime:
XAML
<UserControl .... Background="Black" .... >
Code Behind
public YourUserControl()
{
InitializeComponent();
if( !System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
{
this.Background = Brushes.Transparent;
}
}
Alternate Method
UserControl:
In your user control, do not declare a background color:
<UserControl ... namespaces ...>
UserControl Code Behind:
In your user control's constructor, use the DesignTime method as above, but check to see if it is Design Mode (opposite check from other method):
public YourUserControl()
{
InitializeComponent();
if( System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
{
this.Background = Brushes.Black;
}
}
App.xaml:
Finally, in your App.xaml, add a style to set a background color for UserControls:
<Application.Resources>
<Style TargetType="{x:Type UserControl}">
<Setter Property="Background" Value="Black" />
</Style>
</Application.Resources>
Here's what's happening:
The App.xaml will effect the UserControl at design time because a typed style is applied on an object automatically, but it is not applied to a derived object (UserControl in this case). So, at design time, VS thinks it should apply the style, but at runtime, it will be ignored.
The GetIsInDesignMode check will effect the UserControl when viewing the control in a Window that is using the UserControl because VS is compiling the UserControl at design time in order to render it in the Visual Designer.
HTH's
As shown in this post, you can condense the code to a single style by using a trigger, since DesignerProperties.IsInDesignMode is an attached property.
Actually, the code there isn't quite right. It defines an implicit style for TargetType="{x:Type UserControl}", which would be ignored at runtime anyway because your UserControl is actually a derived class -- as Metro Smurf points out in his first point:
The App.xaml will effect the UserControl at design time because a
typed style is applied on an object automatically, but it is not
applied to a derived object (UserControl in this case). So, at design
time, VS thinks it should apply the style, but at runtime, it will be
ignored.
The right way to do it would be to give it a key and apply it manually to your UserControls:
<Application
...
xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework">
...
<Application.Resources>
...
<Style x:Key="DesignerBlackBackgroundStyle" TargetType="Control">
<Style.Triggers>
<Trigger Property="componentModel:DesignerProperties.IsInDesignMode"
Value="True">
<Setter Property="Background" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
and:
<UserControl x:Class="MyUserControl"
Style="{StaticResource ResourceKey=DesignerBlackBackgroundStyle}">
As a trigger, this has an extra benefit over setting the background in code-behind -- it will behave properly if the background is explicitly set somewhere else, such as from a containing UserControl:
<UserControl x:Class="ContainerUserControl" ...>
...
<local:MyUserControl Background="Gray" />
Local values have precedence over style triggers, so on this screen the designer would use a gray background, whereas it would be black when designing MyUserControl stand-alone.
Are you able to use Blend for designing? Blend has an option to switch between light and dark color schemes.
Set the background color of the usercontrol to black in the XAML, then set it to transparent in code.
Edit:
If you're not comfortable leaving the code this way, then you can revert this change before you release, once you are done with all the designer work, though there is no harm in leaving it in.
Set the XAML Designer background color to Gray.
Tools > Options> Fonts and Colors:
Show settings for: XAML Designer,
Display items: Artboard Background,
Item foreground/background: Gray.
Now you can see text in XAML Designer
It's a shame there is this inconvenience.
How do I set the TextWrapping of every cell in a WPF DataGrid to "NoWrap"? I understand the Cell itself does not have a "TextWrapping" property, but I'd like to set the property on the control within the cell.
The DataGrid I am working with does not have columns defined explicitly, the result set it is displaying is dynamic.
I am looking for a solution similar to the answers provided in the links below. However I do not want to explicitly override the cell style/template and define the control to be used. Instead I would like to say, IF a TextBlock is being used, set its TextWrapping property to NoWrap.
WPF toolkit datagrid cell text wrapping
How do I enable text wrapping on all column headers?
In the resources of your DataGrid, you can specify an alternative default style for TextBlocks. This should do what you require ("IF a TextBlock is being used, set its TextWrapping property to NoWrap"). This won't work if the TextBlocks explicitly specify a different style to be used.
<DataGrid ...>
<DataGrid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
</DataGrid.Resources>
...
</DataGrid>
(Untested, since I do not have Visual Studio available right now.)
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