Set Width based on other control Width - wpf

I am trying to set the Width of an image of 600px width, based on the Width of another control in the same Grid column (Grid.Column="0"). Is there a way where I can capture the Width of that element (MenuControl) and put that number in the image Width to get a lower value than 600px?
<DockPanel Grid.Row="1" Grid.Column="0">
<Image Source="/Assets/combanc.png" Margin="10" Width="{Binding ElementName=MenuControl, Path=ActualWidth}" />
</DockPanel>

If I understand your question correctly, you want to bind the size of another control, but the image should not exceed a maximum size of 600, regardless of the size of the other control. You can create a value converter that returns the minimum of the bound size and a parametrized maximum.
public class MaximumSizeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is double size) ||
!(parameter is string maximumText) ||
!double.TryParse(maximumText, out var maximum))
return Binding.DoNothing;
return Math.Min(size, maximum);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException();
}
}
Then you can create an instance of the converter in a resource dictionary in scope, e.g. Grid.Resources and use it in the Width binding of Image. Specify the converter by referencing it in the Converter property and set the maximum as ConverterParameter.
<Grid>
<Grid.Resources>
<local:MaximumSizeConverter x:Key="MaximumSizeConverter"/>
</Grid.Resources>
<!-- ...your other markup. -->
<DockPanel Grid.Row="1" Grid.Column="0">
<Image Source="/Assets/combanc.png" Margin="10" Width="{Binding ElementName=MenuControl, Path=ActualWidth, Converter={StaticResource MaximumSizeConverter}, ConverterParameter=600}" />
</DockPanel>
</Grid>

Related

Placing a PopUp to the top of the mouse-cursor position

I am working on an interactive chart, where I am displaying a popup with more information, when the user clicks on a data-point.This works fine so far and this is the popup definition:
<Popup IsOpen="{Binding PopupViewModel.IsOpen}"
Placement="Mouse"
HorizontalOffset="-150">
<Popup.Resources>
<DataTemplate DataType="{x:Type ViewModels:DataPointPopUpContentViewModel}">
<Views:DataPointPopUpContentView/>
</DataTemplate>
</Popup.Resources>
<Border BorderThickness="1" BorderBrush="Black" Background="White">
<ContentPresenter Content="{Binding PopupViewModel}" />
</Border>
</Popup>
The default placement of the popup, when using Placement="Mouse" is at the bottom right of the mouse-cursor. However I want the popup to be placed just at the top edge the mouse-cursor. As you can see I have achieved the horizontal centering by setting HorizontalOffset="-150", which is have of the fixed popup-width (Width=300). For the vertical placement I have the problem, that the popup-height is not fixed, since I am displaying an image of variable size and aspect-ratio inside it. I have therefore tried to set VerticalOffset to the ActualHeight of the pupup by adding VerticalOffset="{Binding ActualHeight}". This does not work unfortunately. Any ideas on what I am doing wrong and how to achieve my goal?
First of all you need a converter:
public class MultiplyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double && parameter is double)
{
return ((double)value) * ((double)parameter);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Then try to bind the VerticalOffset property to the ActualHeight of the Popup's child:
<Window.Resources>
<local:MultiplyConverter x:Key="MultiplyConverter" />
<sys:Double x:Key="Factor">-.5</sys:Double>
</Window.Resources>
<Popup IsOpen="{Binding PopupViewModel.IsOpen}"
Placement="Mouse"
HorizontalOffset="-150">
<Popup.VerticalOffset>
<Binding Path="Child.ActualHeight" RelativeSource="{RelativeSource Self}"
Converter="{StaticResource MultiplyConverter}" ConverterParameter="{StaticResource Factor}" />
</Popup.VerticalOffset>
<!-- popup content -->
</Popup>
I hope it can help you.

WPF Binding TextBlock to ProgressBar with Percentage

I have a ProgressBar which has a maximum of 10,000 (not 100). I want the binded TextBox to show the percentage of the current Value, not the Value itself. Is it possible to do this in the .xaml code?
The following shows the current Value of my ProgressBar. I want it to show the Value / Maximum.
<ProgressBar x:Name="calculationProgressBar" />
<TextBlock x:Name="calculationProgressText" Text="{Binding ElementName=calculationProgressBar, Path=Value, StringFormat={}{0:0}%}" />
You could setup a simple IMultiValueConverter and pass in the ProgressBars Value and Maximum properties
Example:
Converter
public class ValueToPercentConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double value = System.Convert.ToDouble(values[0]);
double maximum = System.Convert.ToDouble(values[1]);
return (value / maximum) * 100;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="100" Width="100">
<Window.Resources>
<local:ValueToPercentConverter x:Key="ValueToPercentConverter" />
</Window.Resources>
<StackPanel>
<ProgressBar x:Name="calculationProgressBar" Maximum="10000" Value="2566" Height="40"/>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource ValueToPercentConverter}" StringFormat="{}{0}">
<Binding ElementName="calculationProgressBar" Path="Value"/>
<Binding ElementName="calculationProgressBar" Path="Maximum"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Window>
Result:
Since manipulations are not possible in XAML so converters is a approach for the same
I tried to write a simple code for you, using string format for percent notice StringFormat={}{0:P0}
XAML example
<StackPanel>
<StackPanel.Resources>
<l:ScaleConverter x:Key="ScaleConverter"/>
</StackPanel.Resources>
<Slider x:Name="calculationSource" Maximum="10000"/>
<TextBlock Text="{Binding ElementName=calculationSource,
Path=Value, StringFormat={}{0:P0},
Converter={StaticResource ScaleConverter},
ConverterParameter=10000}" />
</StackPanel>
I used slider instead of progressbar for easy demo, you can use any source
specify the max value in converter parameter
and P0 in string format means percentage format with 0 precision eg 0%, you can choose to make it P1 for 1 decimal and so on eg 0.0%
converter class
class ScaleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return System.Convert.ToDouble(value) / System.Convert.ToDouble(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
converter is pretty simple, divide the value by the defined scale.
I hope you'll find it useful for your issue
Extras
additionally if you prefer to define the max range at a single location you can use resources for the same
<StackPanel>
<StackPanel.Resources>
<l:ScaleConverter x:Key="ScaleConverter"/>
<sys:Double x:Key="maxRange">10000</sys:Double>
</StackPanel.Resources>
<Slider x:Name="calculationSource" Maximum="{StaticResource maxRange}"/>
<TextBlock Text="{Binding ElementName=calculationSource,
Path=Value, StringFormat={}{0:P0},
Converter={StaticResource ScaleConverter},
ConverterParameter={StaticResource maxRange}}" />
</StackPanel>

WPF binding IValueConverter and the width of another control

The following code worked fine. There was an error elsewhere in the code. Still, the advice given is good.
I am trying to bind the Width of a TextBox to a percentage of the Width of a parent control. I know I can accomplish something similar by simply setting a Margin, but I was wondering why this doesn't work.
First, I set a reference to an IValueConverter in the resources collection of my user control:
<UserControl.Resources>
<local:TextBoxWidthConverter x:Key="txtWidthConv" />
</UserControl.Resources>
In the main xaml, I have the following:
<StackPanel Name="parentPanel" Width="300">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Hidden" Name="scroller" Width="{Binding Width,
ElementName=parentPanel, Converter={StaticResource txtWidthConv}}">
<StackPanel Orientation="Horizontal">
<TextBox></TextBox>
</StackPanel>
</ScrollViewer>
</StackPanel>
The ivalueconverter looks like this:
public class TextBoxWidthConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double result = (double)value;
if (!Double.IsNaN(result))
{
result = result * .25;
}
else
{
result = 100D;
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException("Not implemented.");
}
#endregion
}
Setting the width property does nothing here, let alone setting the IValueConverter. I would expect the ScrollViewer to be 1/4 the width of the parent StackPanel.
Set the ScrollViewer's HorizontalAlignment to something other than Stretch.
Also, you should bind to the ActualWidth property.
Let the layout system work for you instead of fighting it. Grid will automatically handle relative sizing:
<StackPanel Name="parentPanel" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Hidden" Name="scroller">
<StackPanel Orientation="Horizontal">
<TextBox></TextBox>
</StackPanel>
</ScrollViewer>
</Grid>
</StackPanel>

Binding to Transforms in a ControlTemplate

I'm trying to create a custom control in Silverlight that dynamically scales an element in it's ControlTemplate. First attempt of the ControlTemplate looks something like this:
<ControlTemplate TargetType="controls:ProgressBar">
<Grid>
<Rectangle x:Name="TrackPart" Fill="{TemplateBinding Background}" HorizontalAlignment="Left" />
<Rectangle x:Name="ProgressPart" Fill="Blue" >
<Rectangle.RenderTransform>
<ScaleTransform ScaleX="{TemplateBinding Progress}" />
</Rectangle.RenderTransform>
</Rectangle>
</Grid>
</ControlTemplate>
However, this forum thread states that TemplateBinding only works on derivatives of FrameworkElements. ScaleTransform is not a FrameworkElement. Is there a work around for this? Any best practices for this sort of situation out there?
Rather than binding the ScaleX and ScaleY properties of the RenderTransform, you can bind the RenderTransform itself.
The problem is that the source is a double value, and you need a Transform. So you need to be able to convert a double to a ScaleTransform. You can create an IValueConverter to do that:
public class TransformConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double)
{
double d = (double)value;
return new ScaleTransform { ScaleY = d, ScaleX = d };
}
else
{
return new ScaleTransform();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
You can't specify an IValueConverter to use in a TemplateBinding, so you can use a regular Binding with RelativeSource as TemplatedParent. Like this:
<Rectangle x:Name="ProgressPart" Fill="Blue"
RenderTransform="{Binding Path=Progress, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource converter1}}" >
and you need to place the IValueConverter in the resources of ControlTemplate's root, in scope of the Binding:
<ControlTemplate TargetType="controls:ProgressBar">
<Grid>
<Grid.Resources>
<local:TransformConverter x:Key="converter1" />
</Grid.Resources>
Assuming that you are always using simple items like a rectangle, you could bind the rectangle's height and width to the progress, and then use a binding converter to adjust the value accordingly

Show the validation error template on a different control in WPF

I have a UserControl that contains other controls and a TextBox. It has a Value property that is bound to the TextBox text and has ValidatesOnDataErrors set to True.
When a validation error occurs in the Value property binding, the error template (standard red border) is shown around the entire UserControl.
Is there a way to show it around the TextBox only?
I'd like to be able to use any error template so simply putting border around textbox and binding its color or something to Validation.HasError is not an option.
Here's my code:
<DataTemplate x:Key="TextFieldDataTemplate">
<c:TextField DisplayName="{Binding Name}" Value="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
</DataTemplate>
<controls:FieldBase x:Name="root">
<DockPanel DataContext="{Binding ElementName=root}">
<TextBlock Text="{Binding DisplayName}"/>
<TextBox x:Name="txtBox"
Text="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsReadOnly="{Binding IsReadOnly}"/>
</DockPanel>
UserControl (FieldBase) is than bound to ModelView which performs validation.
to accomplish this task I've used this solution. It uses converter, that "hides" border by converting (Validation.Errors).CurrentItem to Thickness.
<Grid>
<Grid.Resources>
<data:ValidationBorderConverter
x:Key="ValidationBorderConverter" />
</Grid.Resources>
<Border
BorderBrush="#ff0000"
BorderThickness="{Binding
ElementName=myControl,
Path=(Validation.Errors).CurrentItem,
onverter={StaticResource ValidationBorderConverter}}">
<TextBox
ToolTip="{Binding
ElementName=myControl,
Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Border>
</Grid>
ValidationBorderConverter class is pretty simple:
[ValueConversion(typeof(object), typeof(ValidationError))]
public sealed class ValidationBorderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return (value == null) ? new Thickness(0) : new Thickness(1);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Resources