Databinding WPF Rectangle gradient fill - binding

I'm attempting to databind a gradient brush as a Rectangle fill. What do I need to do to get the commented-out binding to work? StartColor and EndColor are declared as System.Drawing.Color.
<Rectangle Width="100" Height="20">
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Offset="0" Color="Pink"/>
<GradientStop Offset="1" Color="Red"/>
<!--<GradientStop Offset="0" Color="{Binding Path=StartColor}"/>-->
<!--<GradientStop Offset="1" Color="{Binding Path=EndColor}" />-->
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>

The problem was using System.Drawing.Color rather than the proper System.Windows.Media.Color

Related

SVG linearGradient not rendering in iOS Ionic/Angular app

I have the following svg:
<svg
class="cu-onboarding__login-container-gradient-object-bottom"
width="800"
height="314"
viewBox="0 0 375 314"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M59.1055 231.889C92.1114 207.309 133.608 198.845 172.433 186.598C197.996 178.534 219.893 164.86 243.453 152.356C303.352 120.566 376.181 98.9861 424.044 150.47C463.933 193.376 471.775 279.363 427.797 333.873C412.585 352.726 393.211 363.569 372.415 370.098C359.93 374.019 344.434 375.011 333.956 385.621C322.908 396.813 319.771 415.91 315.573 430.508C309.332 452.239 302.685 475.063 289.413 493.687C258.966 536.419 222.943 523.336 192.068 489.499C177.06 473.049 164.539 454.553 150.567 437.257C130.752 412.728 106.278 399.301 78.8891 384.535C54.4984 371.386 29.5248 354.466 20.3018 328.335C9.83631 298.685 23.4443 264.712 46.095 242.903C50.2548 238.898 54.6005 235.244 59.1055 231.889Z"
fill="url(#paint2_linear)"
/>
<defs>
<linearGradient
id="paint2_linear"
x1="225.411"
y1="-47.5534"
x2="-120.158"
y2="88.7281"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#FFBB11" />
<stop offset="1" stop-color="#FF3FA8" />
</linearGradient>
</defs>
</svg>
This renders fine in Chrome, Safari, and Android, but in iOS (built with ionic), it doesn't render. It's just blank.
If i change fill="url(#paint2_linear)" to fill="red", it renders it as a red object.
What is going on? What is wrong with my linearGradient?
the <base href="/"> tag in my index.html was messing things up. I removed it and it worked.

Issues with Xamarin vertical slider

I tried to create a vertical Slider with following code, but Maximum and Minimum track colors don't seem to work on Android (no colors are visible, only thumb color is visible).
<StackLayout Orientation="Vertical"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<RelativeLayout HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Slider
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,Factor=0,Constant=0}"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,Factor=0,Constant=0}"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,Factor=0.15,Constant=0}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width,Factor=1,Constant=0}"
ThumbColor="{StaticResource DataColor}"
MaximumTrackColor="{StaticResource ColorPrimary}"
MinimumTrackColor="{StaticResource ColorPrimaryDark}"
Maximum="100" Minimum="1" Margin="0,0,0,0"
AnchorX="1" AnchorY="1" Rotation="-90"
VerticalOptions="FillAndExpand"/>
</RelativeLayout>
</StackLayout>
I have also tried to customise the slider using a CustomRenderer,
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || e.NewElement == null)
return;
view = (Slider)Element;
Control.ProgressDrawable = Resources.GetDrawable(
Resource.Drawable.custom_progressbar_style,
(this.Context).Theme);
}
And,
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<color android:color="#0066CC"/>
</item>
<item android:id="#android:id/progress">
<clip>
<color android:color="#009900"/>
</clip>
</item>
</layer-list>
But only progress color appears on my view, the background doesn't seems to bet set with value #0066CC.
When I set rotation of my Slider to 0, colors are correctly set. The problem appears when rotation is different from 0.
See following pictures to understand the problem,
Horizontal slider, working as i want
Vertical slider, not working as i want
EDIT : Adding some pictures with different orientations (I think it could help to understand what's wrong)
Orientation set to -10
Orientation set to -45

Multiline text in a ListView (text cut off at the end)

I've see this topic a few times but right now when I need it, I can't find it... I have a problem with a few ListView's in my app. The ListViews are displaying content from a database and from a webservice. The content of the ListView can be very large and sometimes, the text is cut off...
This is my ListView so far:
<ListView Grid.Row="1" x:Name="DataListView" ItemsSource="{Binding GroupData}" GroupShortNameBinding="{Binding ShortCategory}" CachingStrategy="RecycleElement"
IsGroupingEnabled="True" HasUnevenRows="True" BackgroundColor="#b99f65" SeparatorColor="#55492e" ItemTapped="ListView_OnItemTapped" Margin="0,3,0,0">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Grid BackgroundColor="#120305" Margin="0">
<StackLayout Orientation="Horizontal" VerticalOptions="Center" Margin="15,0">
<Label Text="{Binding Category}" TextColor="#b99f65" FontSize="18" FontAttributes="Bold" />
<Label Text="{Binding ShortCategory, StringFormat='({0})'}" FontSize="18" TextColor="#b99f65" />
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Margin="10,5" VerticalOptions="Fill">
<Label Text="{Binding Name}" FontSize="14" FontAttributes="Bold" Margin="0" />
<Label Text="{Binding Effect}" FontSize="11" LineBreakMode="WordWrap" Margin="0" />
<Label Text="{Binding Ingredients, StringFormat='Ingredients: {0}'}" FontSize="11" LineBreakMode="WordWrap" Margin="0,3,0,0" SizeChanged="OnLabelSizeChanged" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Here, the Ingredients are cut off in iOS on some device sizes. I have implemented a SizeChanged-Event for the last Label to see if the size is set correctly, and it is. This is the output:
[0:] LABEL HEIGHT CHANGED:: 26.5
[0:] LABEL HEIGHT CHANGED:: 26.5
[0:] LABEL HEIGHT CHANGED:: 13.5
[0:] LABEL HEIGHT CHANGED:: 13.5
[0:] LABEL HEIGHT CHANGED:: 26.5
[0:] LABEL HEIGHT CHANGED:: 13.5
[0:] LABEL HEIGHT CHANGED:: 26.5
[0:] LABEL HEIGHT CHANGED:: 13.5
[0:] LABEL HEIGHT CHANGED:: 26.5
[0:] LABEL HEIGHT CHANGED:: 13.5
Any of the "26.5" height labels are cut off in half. Because this only happens on iOS it must be a problem there. Can someone help me out to fix this? I have the same issue on other places of my App too and haven't found a working solution yet... I know I can set the RowHeight to the max possible value but for this I need to know how many times the text will be wraped and it is not a nice solution... I would like to use the dynamic sizing...
As a workaround I currently use this on the last Label in iOS, but it takes a huge amount of performance and looks very bad...
private void OnLabelSizeChanged(object sender, EventArgs e)
{
var stackLayout = (sender as Label)?.Parent as StackLayout;
if (stackLayout != null)
{
var childrenHeight = stackLayout.Children.Sum(c => c.Height) + stackLayout.Children.Count * stackLayout.Spacing;
stackLayout.HeightRequest = childrenHeight;
var viewCell = stackLayout.Parent as ViewCell;
viewCell?.ForceUpdateSize();
}
}
And beside of the performance lost it also makes the app looks wrong if the device orientation changes.

TemplateBinding for Dynamic Controls

I create a templated RadioButton at run time. After initializing the templated RadioButton, I set the DataContext and Tag property and then add this button in a StackPanel. The problem is that the template binding does not work. below is the XAML and the code behind. All this work if I assign values to these properties in XAML. Any ideas?
Code:
TemplatedRadioButton commandButton = new TemplatedRadioButton();
commandButton.DataContext = "bla"; // Some txt that I will.
commandButton.Tag = MyImage; // This is the ImageIcon that I create at run time too.
MyStackPanel.Children.Add(commandButton);
XAML:
Sorry:
Code:
TemplatedRadioButton commandButton = new TemplatedRadioButton();
commandButton.DataContext = "bla"; // Some txt that I will.
commandButton.Tag = MyImage; // This is the ImageIcon that I create at run time too.
MyStackPanel.Children.Add(commandButton);
XAML:
<Grid Margin="0 8 0 1">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Name="textheight" Height="2*"/>
</Grid.RowDefinitions>
<ContentPresenter x:Name="Content" ContentSource="Tag" Margin=" 4 4 6 6" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<TextBlock Name="caption" Text="{TemplateBinding DataContext}" FontSize="11" FontFamily="/Fonts/#Lucida Grande" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FF313131" Margin="0 2 0 6"/>
</Grid>
</Border>

Default Layout Orientation when printing XPSs using the WPF XPS Viewer

Is there a way to set the Default Layout Orientation when printing XPSs using the WPF XPS Viewer?
My fixed document XPS has its page orientation set to Landscape, the Page Media Size has a width that is longer that its height and it displays correctly in the Viewer as Landscape.
Its just that when you hit the print button the Print Dialog preferences are defaulted to Portrait and it prints as such.
I'd rather not have to alter the users default print settings I'd much prefer it if the XPS Viewer would print the XPS as it was designed to be printed.
Fill the field of the PrintTicket:
PrintDialog pd = new PrintDialog();
PrintTicket pt = new PrintTicket();
pt.PageOrientation = PageOrientation.Landscape;
pd.PrintTicket = pd.PrintQueue.MergeAndValidatePrintTicket(pd.PrintQueue.DefaultPrintTicket, pt).ValidatedPrintTicket;
if (pd.ShowDialog() == true)
{
...
}
I believe the correct way to do this when creating a FixedDocument, is set the RenderTransform = RotateTransform(90) on the page content when the dimensions are higher than they are wide.
Example:
var visualContent = new Image
{
Source = image,
Stretch = Stretch.Uniform
};
visualContent.RenderTransformOrigin = new Point(0.5, 0.5);
visualContent.RenderTransform = new RotateTransform(90);
FixedPage fixedPage = new FixedPage();
fixedPage.Children.Add(visualContent);
var pageContent = new PageContent
{
Child = fixedPage
};
Not sure if that helps with a pre-existing XPS document however.
This is not really a problem with MXDW but one with the way drivers work on Windows. The user choice(s) is/are saved for a particular session. This means that you can reuse firs-print settings when printing between the first print and quitting the application. Most printers behave this way, until unless one comes up with a way to save this information somewhere and let the user re-use it across sessions.
So, I tried hacking the GPD file (where printing information for a printer is typically stored). The orientation has two possible values: PORTRAIT and LANDSCAPE_CC270 with the default being set to PORTRAIT. See below:
*%******************************************************************************
*% Orientation
*%******************************************************************************
*Feature: Orientation
{
*rcNameID: =ORIENTATION_DISPLAY
*DefaultOption: PORTRAIT
*Option: PORTRAIT
{
*rcNameID: =PORTRAIT_DISPLAY
}
*Option: LANDSCAPE_CC270
{
*rcNameID: =LANDSCAPE_DISPLAY
}
}
Now, if I were to change swap the default value to LANDSCAPE_CC270, the printing preferences stop coming up (and any print would fail). In fact, it appears, that specifying any other value keeps the default to PORTRAIT. Definitely, MS is
doing some sort of check to prevent us from hacking this driver. Looks like MS doesn't
want anyone to tamper with its settings :(
But you could try flirting with the GPD values a bit more and see if something of your liking comes up. Will keep hacking a bit more.
Caveat: GPD files shouldn't be tampered with if you don't know what you are doing. If you
still want to go ahead make a backup!
Hint: They are stored in %WINDOWS%system32\spool\drivers\w32x86\3 folder.
<Grid Margin="0,0,-8,-8">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<FlowDocumentScrollViewer Name="printpanel" HorizontalAlignment="Left" Width="959" FontFamily="Arial" Margin="0,-10,0,10">
<FlowDocument x:Name="FD">
<BlockUIContainer>
<Canvas>
<Label x:Name="lblReceipt" Visibility="Visible" Content="Receipt No." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Top="178" FontSize="12" Canvas.Left="60"/>
<Label x:Name="txtReceiptNo" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left" Padding="10,3,3,0" Height="23" VerticalAlignment="Top" Width="200" FontSize="12" Canvas.Left="187" Canvas.Top="177" FontFamily="Arial"/>
<Label x:Name="lblmemNo" Visibility="Visible" Content="Membership No." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="462" Canvas.Top="177"/>
<Label x:Name="txtMembershipNo" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left" Padding="10,3,3,0" Height="23" VerticalAlignment="Top" Width="177" FontSize="12" Canvas.Left="604" Canvas.Top="177" FontFamily="Arial">
</Label>
<Label x:Name="lblAuthCentr" Visibility="Visible" Content="Authorised Center." HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="60" Canvas.Top="221" FontSize="12"/>
<TextBox x:Name="txtAuthCentr" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Padding="10,3,3,0" Height="38" VerticalAlignment="Top" Width="219" FontSize="12" Canvas.Left="238" Canvas.Top="219" FontFamily="Arial"/>
<Label x:Name="lblSector" Visibility="Visible" Content="Sector." HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12" Canvas.Left="492" Canvas.Top="220"/>
<Label x:Name="txtSector" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Left" Padding="10,3,3,0" Height="23" VerticalAlignment="Top" Width="115" FontSize="12" Canvas.Left="567" Canvas.Top="220" FontFamily="Arial"/>
</Canvas>
</BlockUIContainer>
</FlowDocument>
</FlowDocumentScrollViewer>
<Button Name="btnOk" Content="Print" Height="30" Grid.Row="1" Click="btnOk_Click" Margin="355,0,404,0"></Button>
</Grid>
Just set FlowDocument Height and width
set FD.PageWidth = 1100; FD.PageHeight = 600;
private void btnOk_Click(object sender, RoutedEventArgs e)
{
if (File.Exists("printPreview.xps"))
{
File.Delete("printPreview.xps");
}
var xpsDocument = new XpsDocument("printPreview.xps", FileAccess.ReadWrite);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
DocumentPaginator docPage;
FD.PageWidth = 1100; // set FlowDocument Width
FD.PageHeight = 600; // set FlowDocument Height
docPage = ((IDocumentPaginatorSource)FD).DocumentPaginator;
writer.Write(docPage);
Document = xpsDocument.GetFixedDocumentSequence();
this.Close();
xpsDocument.Close();
var windows = new PrintWindow(Document);
windows.ShowDialog();
}

Resources