xaml change resolution changes the view's size - wpf

I have a view that is fixed in height and width, and im currently using a 4k monitor. when i run my application on a 1080p monitor, its blew things up. i have some lines and paths that are set at a certain margin, and so is other controls.
i have tried binding to the screen width and height (see below), but it didn't kept the screen size fix when change to other resolution. i also set ResizeMode to NoResize
Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={k1:RatioConverter}, ConverterParameter='0.7'}"
Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={k1:RatioConverter}, ConverterParameter='0.7'}"
here is the converter i got from #berhauz
[ValueConversion(typeof(string), typeof(string))]
public class RatioConverter : MarkupExtension, IValueConverter
{
private static RatioConverter _instance;
public RatioConverter() { }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ // do not let the culture default to local to prevent variable outcome re decimal syntax
double size = System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
return size.ToString("G0", CultureInfo.InvariantCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ // read only converter...
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return _instance ?? (_instance = new RatioConverter());
}
}
is there a way to fix the screen size on every resolution so the lines,paths,controls will not jump around?

i found the simple solution was to wrap everything in the viewbox and bind the height and width of the viewbox to the primary screen
Height="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}, Converter={k1:RatioConverter}, ConverterParameter='0.7'}"
Width="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}, Converter={k1:RatioConverter}, ConverterParameter='0.7'}"
no matter what resolution you have, the contents inside my viewbox stayed the same and does not change sizes

Related

How to reference a sub class for IValueConverter?

I have read this SOF Post: How to properly reference a class from XAML
But i cannot get this work.
Because my converter class is subclass, i cannot get reference on XAML.
The Converter Class:
using System;
using System.Windows;
using System.Windows.Data;
namespace GapView.Classes
{
public class ConverterClass
{
public class PhotoBorderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int width = System.Convert.ToInt32(value);
return width + 16;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int width = System.Convert.ToInt32(value);
return width - 16;
}
}
}
}
And the MainWindow.xaml, XML section, i add
xmlns:GapView="clr-namespace:GapView"
xmlns:Classes="clr-namespace:GapView.Classes"
Inside
<Classes:ConverterClass x:Key="BorderConverter" />
Finally, i apply to Border element.
And SettingThumbWidth is a TextBox element.
<Border Width="{Binding Path=Text , ElementName=SettingThumbWidth, Converter={StaticResource BorderConverter}}" Height="166" >
When i press "." after BorderConverter, the subclass PhotoBorderConverter won't show and it seems i cannot access.
So how can i fix this?
It because it possible have other Converter, so i want to centralized in one ConvertClass.
Thanks you.
Your decision to centralize into ConverterClass is kind of weird and unnecesary. You can keep all your converters in one file, but you don't need to encapsulate them within an outer class.
With what you currently have, try using the correct namespace like this:
xmlns:converters="clr-namespace:GapView.Classes.ConverterClass"
<converters:PhotoBorderConverter x:Key="BorderConverter" />

How do I bind a DataGrid Width to a converter?

I want to include the size of the VerticalScrollbar when i define the width of a DataGrid.
So far i wrote a Converter:
[ValueConversion(typeof(double), typeof(double))]
public class VerticalScrollbarConverter : IValueConverter
{
#region IValueConverter Member
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is double)
return (double)value + SystemParameters.VerticalScrollBarWidth;
else
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I included my converter in the xaml namespace with:
xmlns:Core="clr-namespace:Core;assembly=SMS_Core"
And I defined the converter as a (window) resource:
<Window.Resources>
<Core:VerticalScrollbarConverter x:Key="VerticalScrollbarConverter"/>
</Window.Resources>
Since all of my DataGrid.Columns have a fixed Value I know the value that i need to pass.
How do I tell my GridView Width property in xaml to use the converter?
I know that my question is pretty basic. As you can tell I am very new to WPF.
Thanks in advance for every hint. If you need some more info or context just ask away.
The comment from sa_ddam213 didn't exactly solve the problem. But it sure did push me into the right direction.
To pass the value automatically I had to do the following:
I created a property in the window class called TotalColumnWidth. Returning this:
myDataGrid.Columns.Sum(c => c.ActualWidth);
The xaml is the following:
Width="{Binding ElementName=_Root, Path=TotalColumnWidth, Converter={StaticResource ResourceKey=VerticalScrollbarConverter}}"
Using the converter mentioned above.

How to get the value of an inherited FontSize property in an UserControl?

I am working on Windows Phone 7.1 application.
I have a UserControl which has a bunch Silverlight child controls. The font sizes for most of these child controls can be same as that of the host of the said UserControl (which will anyway get inherited from the host).
However for one of the child controls, I want to set the FontSize to half the FontSize of the UserControl. And I want it to update if the FontSize of the host changes.
But now during the UserControl design time, since there is no host, I am unable make this work. Doing something like 0.5*FontSize results in some default value of FontSize and it does not get updated when the FontSize changes.
What should I be doing to make it work?
You can bind your FontSize property to the FontSize property of your UserControl and use a converter to do the ratio.
Here's a sample with a textblock inside a page:
<TextBlock FontSize="{Binding ElementName=MyPage, Path=FontSize,
Converter={StaticResource FontSizeConverter}}"
Text="any text" />
And the converter (declared as a resource in the App.xaml file:
public class FontSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var valueToConvert = value == null ? 0 : System.Convert.ToDouble(value);
return valueToConvert * 0.5;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException("no use");
}
}

how to showing time elapsed in a media element in silverlight?

how can i showing time elapsed of a video file who is playing in a MediaElement control in silverlight?
There is a Position property on the MediaElement. This video and source code should help you out http://www.silverlight.net/learn/quickstarts/audioandvideo/
Finally i resolve my problem with this way:
i use a Textblock control and bind text property to Position property of MediaElement control, then i use a IvalueConverter to show appropriate Time in TextBlock:
<TextBlock MinWidth="40" Text="{Binding ElementName=myMediaElement, Path=Position, Converter={StaticResource TimeConverterFormatter}}"/>
TimeConverterFormatter is a class for convert TimeSpan value to short time format. because position property in MediaElement show a Timespan value like to "0:00:00:00.0000000" and i want elapsed time value like to this: "00:00"
public class TimeConverterFormatter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.ToString().Length == 16)
return value.ToString().Substring(3, 2) + ":" + value.ToString().Substring(6, 2);
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I realise that this is an old question now, but in WPF you can just use a normal StringFormat to do what you want:
<TextBlock Text="{Binding Position, ElementName=myMediaElement,
StringFormat={}{0:hh}:{0:mm}, FallbackValue=00:00, Mode=OneWay}" />
I couldn't work out whether you wanted hours and minutes or minutes and seconds, so here is the latter:
<TextBlock Text="{Binding Position, ElementName=myMediaElement,
StringFormat={}{0:mm}:{0:ss}, FallbackValue=00:00, Mode=OneWay}" />
I can't guarantee that it will work in Silverlight though.
Not sure about Silverlight but this should work to show mm:ss.
lblLength.Content = String.Format("{0:mm}:{0:ss}", mediaElement.Position);

Using Resourse.resx in WPF application to set color

I´m trying to make an application that has to be able to easily change a dll file which could change colors in the application.
I´m trying to use resource manager to do this but am having problems with setting color values so that the styles for views can easily accept it.
We know that(in this case) the background of a button takes in SolidColorBrush, and while
Value="Black" works,
Value={x:Static res:AppResources.Btn_Background}
which gives the string Black does not (current theory being that converters make the former work but not the latter).
This is all being done in wpf & mvvm.
Have you guys an idea about how this could be done.
Greetings
You could use a Binding:
Background="{Binding Source={x:Static res:AppResources.Btn_Background}}"
This will cause the CoerceValue to fire for the DependencyProperty controlling the background.
#Snowbear mentioned it may be a Color rather than a String, in which case you would need to provide a trivial IValueConverter.
public class ColorConverter: IValueConverter
{
#region IValueConverter Members
private Dictionary<Color, Brush> brushes = new Dictionary<Color, Brush>();
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
Brush brush;
var color = (Color)value;
if (!brushes.TryGetValue(color, out brush))
{
brushes[color] = brush = new SolidColorBrush(color);
brush.Freeze();
}
return brush;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Your specific issue is that you are bypassing the default string to Brush conversion and would need to handle that manually.
As sixlettervariables states, you'd can use a Binding if your source is a string, but that is probably overkill. At a minimum, you'd want to set Mode=OneTime on the Binding.
You can also create a custom MarkupExtension that performs the conversion.
Your conversion, whether it be through a custom IValueConverter or MarkupExtension, can leverage the BrushConverter class. So things like "Black" or "#000" will work as they do when defining the color in XAML like your first example.
EDIT:
Actually a markup extension that derives from StaticExtension, makes this easier:
public class BrushStaticExtension : StaticExtension {
private static BrushConverter converter = new BrushConverter();
public BrushStaticExtension() { }
public BrushStaticExtension(string member) : base (member) { }
public override object ProvideValue(IServiceProvider serviceProvider) {
return converter.ConvertFrom(base.ProvideValue(serviceProvider));
}
}
If you specify a string then XAML parser uses a converter from string which automatically creates a SolidColorBrush. As far as I understand at the moment Btn_Background resource is Color but it should be a SolidColorBrush instead.

Resources