Line break in ResourceDictionary string not working - xaml

I have the following:
<sys:String x:Key="NoDeviceAlert" xml:space="preserve">Your device is currently disabled.
Please ensure it is turned on and connected.</sys:String>
However it doesn't work. The TextBlock:
<TextBlock Text="{DynamicResource NoDeviceAlert}" Style="{DynamicResource msgTextStyle}" HorizontalAlignment="Center" />
keeps both sentences on a single line, but removes the decimal values. I have also tried hex values as well as /r/n.
How can this not work?
UPDATE
<Style x:Key="msgTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="#FFC8DBE7" />
<Setter Property="FontFamily" Value="/Project;component/Utilities/Resources/Fonts/frutiger.ttf#Frutiger Linotype" />
<Setter Property="FontSize" Value="20" />
<Setter Property="TextAlignment" Value="Left" />
</Style>
ResourceDictionary sys namespace:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
The application is for Windows desktop and using .net 4.5.

The attribute xml:space="preserve" affects only working of XML parser. So line breaks, tabs and spaces will stay preserved.
Instead of typing explicit newline chars just press enter:
<sys:String x:Key="NoDeviceAlert" xml:space="preserve">Your device is currently disabled.
Please ensure it is turned on and connected.</sys:String>

You can try use
&#x 0a;
like a line break (without this space inside).
It should be work.

Related

XAML reuse specific UI elements

With Xamarin, I have a small UI element which acts as a content divider:
<BoxView StyleClass="contentDivider"
HeightRequest="2"
WidthRequest="1000"
Margin="3, 0"/>
Since I use this a number of times I wanted to be able to have the code written down once, and reuse that code - just like a class with its instance (DRY). It's most likely me being a blind bat and not being able to find how it's done. So, how can I reuse XAML elements?
You can do this with ContentViews (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/controls/layouts#contentview), which probably works better for larger reuse cases (using more XAML in the ContentView).
Yet, for such a small single element example as yours, you could really just consider using a global style (https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/application) which its looks like you already have with StyleClass="contentDivider", as long as you only want to override properties on a single element (like your BoxView).
Just add HeightRequest, WidthRequest and Margin to your style and your done.
<Style x:Key="contentDivider" TargetType="BoxView">
<Setter Property="HeightRequest" Value="20" />
<Setter Property="WidthRequest" Value="20" />
<Setter Property="Margin" Value="0,99,0,0" />
... etc
</Style>

Xamarin Forms DatePicker Format Style

I have run out of ideas for trying to change the DatePicker Format property in XAML using OnIdiom. I have tried using XAML styles, I have tried defining the string in a Constants.cs file and referencing it from XAML. The only other way I can think of would be to extended the DatePicker and set it in there. Has anyone gotten this to work?
Things that did not work are listed below.
Global Style with OnIdiom:
<Style TargetType="DatePicker">
<Setter Property="Format">
<OnIdiom x:TypeArguments="x:String"
Tablet="D"
Phone="d"/>
</Setter>
</Style>
Global style referencing a string defined in my Constants.cs class file, which uses Device.Idiom == TargetIdiom.Phone ? "d" : "D";
<Style TargetType="DatePicker">
<Setter Property="Format" Value="{x:Static map:Constants.LongDateTimeStringFormat}"/>
</Style>
Trying to only define the string with a key:
<OnIdiom x:Key="LongDateTimeStringFormat"
x:TypeArguments="x:String"
Tablet="D"
Phone="d"/>
Trying to only define the string with a key another way:
<x:String x:Key="LongDateTimeStringFormat">
<OnIdiom x:TypeArguments="x:String"
Tablet="D"
Phone="d"/>
</x:String>
It also does not work when trying to use <OnIdiom> within the <DatePicker.Format> directly, without styles, or trying to set the DatePicker.Format to {x:Static map:Constants.LongDateTimeStringFormat}.
Well... it seems to be working now in a different project. Adding the following global style does not give me a format exception like it did yesterday.
<Style TargetType="DatePicker">
<Setter Property="Format">
<OnIdiom x:TypeArguments="x:String"
Tablet="D"
Phone="d"/>
</Setter>
</Style>
May have to investigate a little later.

UWP ResourceDictionary Style Error: The parameter is incorrect

I'm making a ResourceDictionary of common styles that are used throughout my application and one of them is:
<Style x:Key="ME_BASE_AppbarButtonSaveStyle"
TargetType="AppBarButton">
<Setter Property="Label"
Value="Save" />
<Setter Property="ToolTipService.ToolTip"
Value="Save" />
<Setter Property="Icon">
<Setter.Value>
<FontIcon FontFamily="Segoe MDL2 Assets"
Glyph="" />
</Setter.Value>
</Setter>
</Style>
It's all ok if I apply the style only one AppbarButton on the Page, but if I want to have two buttons with the same style, I get the following error:
The parameter is incorrect
It's of ok (no error) if I remove the icon property out of the style...
But that's kind of missing the point...
Anyone experienced something similar? Perhaps...
Thank you for all the help.
Error HRESULT E_Fail has been returned from a call to a COM component.
This error will occurred when you use this style for the second AppBarButton. This error usually happens when a reference to a style or an event handler that does not exist or is not with the context of the XAML, you can see the exception information of your problem:
If you read this document: XAML resources must be shareable, you will find:
Custom types used as resources can't have the UIElement class in their inheritance, because a UIElement can never be shareable (it's always intended to represent exactly one UI element that exists at one position in the object graph of your runtime app).
Whether a Icon property of AppBarButton or a FontIcon derives from UIElement, so I guess this is the reason why can't this property be styled in the resource dictionary.
Besides, I will consider if this is a right direction to define the Icon property for each AppBarButton in the style, normally I'd like give each button a different icon as content.
But if you insist to do this, I can provide you a workaround method by defining the Content of the AppBarButton, this is the construction of your AppBarButton:
You use a FontIcon as the content of the AppBarButton, so we can modify your style like this:
<Style x:Key="ME_BASE_AppbarButtonSaveStyle" TargetType="AppBarButton">
<Setter Property="Label" Value="Save" />
<Setter Property="ToolTipService.ToolTip" Value="Save" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<FontIcon FontFamily="Segoe MDL2 Assets"
Glyph="" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

Windows Phone: how to change the Application style programmatically

In my Windows Phone 8 app, I have some implicit styles defined in a xaml file at the location /Styles/DefaultStyles.xaml
I have a similar file but with different colors, fonts, etc ... defined at /Styles/GreenStyles.xaml.
I reference the default style file in my App.xaml as follows :
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/DefaultStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I want to make my app switch its implicit styles from the other styles file (GreenStyles) programmatically.
How can I achieve this ?
**
UPDATE:
I manged to change the source of the resource dictionary as follows:
ResourceDictionary style = App.Current.Resources.MergedDictionaries.ToList()[0];
string source = String.Format("/ApplicationName;component/Styles/GreenStyles.xaml");
style.Source = new Uri(source, UriKind.Relative);
Note: the word component must be written like that to avoid exceptions
Now I have an issue:
only the Implicit styles (the ones that do not have a x:Key attribute) are switched when the source of the dictionary changes.
any other style with a specified key and defined twice (with different attributes) in both files, will not be reflected in the UI.
so if I have these files:
DefaultStyles.xaml:
<Style x:Key="MainGrid" TargetType="Grid">
<Setter Property="Background" Value="Red"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontSize" Value="24"/>
</Style>
</ResourceDictionary>
And:
GreenStyles.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone">
<Style x:Key="MainGrid" TargetType="Grid">
<Setter Property="Background" Value="Green"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="FontSize" Value="24"/>
</Style>
</ResourceDictionary>
and I switched the source to point to GreenStyles.xaml, any Grid with the style MainGrid will still have it's background to Red.
What can be the reason for this ?
You can try using the approach Jeff Wilcox described here: http://www.jeff.wilcox.name/2012/01/phonethememanager/
Alternative approach is described here for Silverlight and I'm not sure if this will work on Windows Phone (though they share some codebase):
http://silverlightips.wordpress.com/2010/04/29/change-themestyle-using-merged-dictionaries/
Both of the ways are not easy if you have a large app and you may consider another option like (call me crazy)
<Button Style="{Binding Locator.Theme, Converter={StaticResource StyleThemeConverter}, ConverterParameter=RefreshButtonStyle}"
Hope this helps.

DXGrid change color of grid row DevExpress WPF

I have entity with property IsRemoved. When it is become true grid row should be Gray.
To do this I am using this code:
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:GridRowContent}">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsRemoved, Mode=OneWay}" Value="True">
<Setter Property="Background" Value="Gray" />
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
But It will run only when grid shows first time. I want to change color when value is changing. Property implement INotifyPropertyChange Event.
Note: this answer is legacy (see my other answer).
This answer is for DevExpress versions prior to v14.1, or DevExpress versions v14.1 and after with
UseLightweightTemplates="None".
You need to have an initial setter for the property you want to change. This is due to the order in which WPF uses styles.
Include this line after your style tag:
<Setter Property="Background" Value="Black" />
Full Example:
<dxg:TableView.RowStyle>
<Style TargetType="{x:Type dxg:GridRowContent}">
<Setter Property="Background" Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsRemoved, Mode=OneWay}" Value="True">
<Setter Property="Background" Value="Gray" />
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
Starting with v14.1 of DevExpress, they introduced Optimized Mode which uses Lightweight Templates. This makes everything faster, but requires a change to how the styles and DataTriggers are specified.
Lightweight Templates are controlled by a the attached property UseLightweightTemplates="Row", which is on by default. It can be switched to None for backwards compatibility.
Here is a working MVVM example of how to color a row if the IsDirty property is set for any grid row.
<dxg:GridControl x:Name="MyGridControl"
ItemsSource ="{Binding MyViewModelList}"
SelectionMode="None"
VerticalAlignment="Stretch">
<dxg:GridControl.Resources>
<SolidColorBrush x:Key="GridRowIsDirty" Color="#FF602D2D" />
</dxg:GridControl.Resources>
<dxg:GridControl.View>
<dxg:TableView UseLightweightTemplates="Row" >
<dxg:TableView.RowStyle>
<Style TargetType="dxg:RowControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Row.IsDirty}" Value="True">
<Setter Property="Background" Value="{StaticResource GridRowIsDirty}" />
</DataTrigger>
</Style.Triggers>
</Style>
</dxg:TableView.RowStyle>
</dxg:TableView>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn x:Name="Included" FieldName="Included"/>
<dxg:GridColumn x:Name="ColumnB" Header="Column B" FieldName="ColumnB" ReadOnly="True"/>
<dxg:GridColumn x:Name="ColumnC" Header="Column C" FieldName="ColumnC" ReadOnly="True"/>ReadOnly="True"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
In the ViewModel behind this grid:
public ObservableCollection<MyViewModel> MyViewModelList { get; set; }
Every row in the grid points to a class of type MyViewModel, which contains a custom IsDirty flag which we can set on demand:
public bool IsDirty
{
get { return _isDirty; }
set
{
_isDirty = value;
OnPropertyChanged();
}
}
Appendix A: Additional Links
See DevExpress: How to disable focused/selected row colors.
See DevExpress: Optimized Mode.
See DevExpress: DXGrid: DataTrigger does not seem to work with UseLightweightTemplates="All".
See DevExpress: Binding to the RowData.Row property is not updated when changing a specific data row property.
See DevExpress: DxGrid: Grid does not update until I scroll the row on off and one the screen.
Appendix B: Other solutions
This also works most of the time, but it will not work if the source of the event is via a context menu, so it is not recommended:
<DataTrigger Binding="{Binding DataContext.IsDirty}" Value="True">
<Setter Property="Background" Value="{StaticResource GridRowIsDirty}" />
</DataTrigger>
Appendix C: AllowLiveDataShaping
If the trigger is not firing, try switching on AllowLiveDataShaping="True" in <GridControl>. However, try to avoid this as it (theoretically) has an impact on the speed of large, complex grids (it has no discernable impact on most grids of a reasonable size).
Appendix D: If all else fails, use a custom ControlTemplate
With the introduction of "UseLightweightTemplates", DevExpress has been focusing on speed. However, the techniques used for speed involve switching off bindings that might slow things down.
This means that if we change something in a DxGrid cell, the value in the ViewModel does not change until the user shifts to the next cell or row. This means that the ViewModel lags behind what is actually in the grid.
To fix this, the only solution that I could find was to bypass DevExpress's templates entirely, and use my own. This means that the DxGrid has no choice but to display a custom template which updates the ViewModel instantaneously as soon as the user edits it, which means that the row color changes immediately:
<dxg:GridControl Grid.Row="3" x:Name="TrsGridControl"
ItemsSource ="{Binding MyObservableCollection}"
VerticalAlignment="Stretch"
AllowLiveDataShaping ="True">
<dxg:GridControl.Resources>
<converter:TestConverter x:Key="TestConverter" />
<ControlTemplate x:Key="DisplayedOnTicketTrs">
<dxe:CheckEdit x:Name="DisplayedOnTicketCheckEdit" HorizontalAlignment="Center" IsChecked="{Binding RowData.Row.DisplayedOnTicket, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</ControlTemplate>
</dxg:GridControl.Resources>
<dxg:GridControl.View>
<dxg:TableView UseLightweightTemplates="All"/>
</dxg:GridControl.View>
<dxg:GridControl.Columns>
<dxg:GridColumn x:Name="DisplayedOnTicketTrs" DisplayTemplate="{StaticResource DisplayedOnTicketTrs}" Header="Displayed On Ticket?" HeaderToolTip="Displayed On Ticket?" AllowEditing="False"/>
Header ="Displayed On Ticket?"/>
<dxg:GridColumn x:Name="ColumnA" Header="ColumnA" FieldName="ColumnA" ReadOnly="True"/>
<dxg:GridColumn x:Name="ColumnB" Header="ColumnB" FieldName="ColumnB" ReadOnly="True"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
After I made this change, everything started to work:
When the checkbox is clicked, the background color changes instantly (if we add the trigger to change the background color, above).
Editing the DxGrid changes the ViewModel instantaneously.
Changing the ViewModel updates the DxGrid instantaneously.
If a ContextMenu updates the ViewModel, then everything just works.
you should write just "Row" instead of "DataContext"