Specific text box string format for binding double property - wpf

I wrote a pretty simple WPF application.
I'm trying to bind a double property to text box text using a custom string format. Here is the code for view model and the code for window.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace StringFormat
{
internal class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
DoubleProperty = 0;
}
private double _doubleProperty;
public double DoubleProperty
{
get { return _doubleProperty; }
set
{
_doubleProperty = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
<Window x:Class="StringFormat.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:StringFormat"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0"
Grid.Column="0"
Width="120"
Height="25"
TextAlignment="Center"
Text="{Binding DoubleProperty, StringFormat={}{##.##}, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
</Window>
namespace StringFormat
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
}
When I run the application it works but I get the following error in console:
System.Windows.Data Error: 6 : 'StringFormat' converter failed to convert value '0' (type 'Double'); fallback value will be used, if available. BindingExpression:Path=DoubleProperty; DataItem='ViewModel' (HashCode=486165); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'
I tried to change the string format and the error did not showed up but I think that is a little annoying to have a '0.0' when you try to delete the content of text box.
I don't know what to do in order to solve the error but also to be able to delete text box text without having that '0.0'.
Can you give me some advice how to deal with this situation?
Thanks!

try this
StringFormat={}{0:#.##}
you forgot 0: in your second curly braces couple
Then to not have "0.0" if textbox.Text is null or empty (i.e. if you delete it) you may try this:
Text="{Binding DoubleProperty, TargetNullValue={x:Static System:String.Empty}, StringFormat={}{0:##.##}, UpdateSourceTrigger=PropertyChanged}"
TargetNullValue={x:Static System:String.Empty} will set the default string value to empty if TextBox.Text = null.
But you may need to set your DoubleProperty nullable.
Note -- You may read this, it help me for lots of wpf question: wpf-tutorial.com

Try StringFormat=##.## then the Text is empty if the value is 0.0

Related

How do dependency properties work in WPF?

I'm trying to understand dependency property and learn how to use it. I'm going through articles and in this article https://www.c-sharpcorner.com/UploadFile/6d590d/wpf-dependency-property/ there's this example:
MainWindow.xaml:
<Window x:Class="WpfApplication1.DependencyPropertyDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="DependencyPropertyDemo" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<local:CarDependencyClass x:Key="carDependencyClass"></local:CarDependencyClass>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Enter Car:" Grid.Row="0" VerticalAlignment="Center" />
<TextBox Text="{Binding Path=MyCar, Source={StaticResource carDependencyClass }}" Name="MyTextCar" Height="25" Width="150" />
<Button Name="MyButton" Content="Click Me!" Height="25" Click="MyButton_Click" Width="150" Grid.Row="1" />
</Grid>
</Window>
MainWindow.xaml.cs:
using System.Windows;
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for DependencyPropertyDemo.xaml
/// </summary>
public partial class DependencyPropertyDemo : Window {
public DependencyPropertyDemo() {
InitializeComponent();
}
private void MyButton_Click(object sender, RoutedEventArgs e) {
CarDependencyClass dpSample = TryFindResource("carDependencyClass") as CarDependencyClass;
MessageBox.Show(dpSample.MyCar);
}
}
public class CarDependencyClass : DependencyObject {
//Register Dependency Property
public static readonly DependencyProperty CarDependencyProperty = DependencyProperty.Register("MyProperty", typeof(string), typeof(CarDependencyClass));
public string MyCar {
get {
return (string)GetValue(CarDependencyProperty);
}
set {
SetValue(CarDependencyProperty, value);
}
}
}
}
It works. I noticed that they registered dependency property with the name "MyProperty" and that it isn't used anywhere in the program. Only normal CLR property MyCar is used in xaml.
But then there's another article https://www.c-sharpcorner.com/article/simplest-wpf-dependency-property-for-beginners-on-background-color/. And they provide other example:
MainWindow.xaml:
<Window x:Class="DependencyPropertyTutorial.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:views="clr-namespace:DependencyPropertyTutorial" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DependencyPropertyTutorial" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="BG" Color="Green" />
</Window.Resources>
<Grid>
<views:CustomButtonControl SetBackground="{DynamicResource BG}"></views:CustomButtonControl>
</Grid>
</Window>
CustomButtonControl.xaml:
<UserControl x:Class="DependencyPropertyTutorial.CustomButtonControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DependencyPropertyTutorial"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button x:Name="btnCustom" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Height="52" Click="btnCustom_Click" />
</Grid>
</UserControl>
CustomButtonControl.xaml.cs:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace DependencyPropertyTutorial {
/// <summary>
/// Interaction logic for CustomButtonControl.xaml
/// </summary>
public partial class CustomButtonControl : UserControl {
public CustomButtonControl() {
InitializeComponent();
}
public static readonly DependencyProperty btnDependencyProperty = DependencyProperty.Register("SetBackground", typeof(SolidColorBrush), typeof(CustomButtonControl), new PropertyMetadata(new SolidColorBrush(Colors.HotPink), new PropertyChangedCallback(OnSetColorChanged)));
public SolidColorBrush SetBackground {
set {
SetValue(btnDependencyProperty, value);
}
get {
return (SolidColorBrush)GetValue(btnDependencyProperty);
}
}
private void btnCustom_Click(object sender, RoutedEventArgs e) {
this.SetBackground = new SolidColorBrush(Colors.IndianRed);
}
private static void OnSetColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
CustomButtonControl mycontrol = d as CustomButtonControl;
mycontrol.callmyInstanceMethod(e);
}
private void callmyInstanceMethod(DependencyPropertyChangedEventArgs e) {
btnCustom.Background = (SolidColorBrush)e.NewValue;
}
}
}
And here they register dependency property with the name "SetBackground" which is exactly the same as the name of CLR property - SetBackground. And if I change dependency property(the one I registered with Register method) "SetBackground" to something else, like "SetBackgroundDependencyProperty", then I get a XAML exception when trying to run the application. But "SetBackground" dependency property isn't even referenced in xaml anywhere. Only CLR property SetBackground is referenced in XAML at the line
<views:CustomButtonControl SetBackground="{DynamicResource BG}"></views:CustomButtonControl>
I also get an error in Visual Studio with this example:
But when I try to build and run the application, it works.
So my questions are: why in the first example they didn't have to name registered dependency property the same as CLR property, but in the second example, I have to name registered dependency property the same as CLR property. Is there a way to name registered dependency property differently to the CLR property in the second example? Why and how xaml even uses dependency properties, considering xaml references only CLR properties anyway? I checked it and in both projects only CLR properties are referenced from XAML, according to VS IntelliSense. Why do I have to register dependency property with the same name as CLR property - "SetBackground" when in xaml only CLR property is referenced and it returns SolidColorBrush from the dependency property, anyway:
return (SolidColorBrush)GetValue(btnDependencyProperty);
Here's the solution with both examples:
https://github.com/KulaGGin/DependencyProperty
First example is a bit dirty, I wouldn't code it such way. There is a good convention to avoid a confusion - to name the DP as the CLR property + 'Property'(but it's not mandatory!) and register it as name of CLR property(if you want to use it as DP in XAML).
First to your questions:
First example does work, because of everywhere, where the property MyCar being used, it is used as CLR property. If you will try to bind to the MyCar, it will fail, because of there is no such a dependency property. To implement the functionality in this example would be enough just to declare a CLR property:
public string MyCar { get; set; }
instead of all this confusion with dependency property.
In second example CLR property as well as dependency property SetBackground are defined (the name btnDependencyProperty for the field is not convenient, but OK). Missunderstanding on your side, is what being used in XAML.
If you use in XAML Binding or DynamicResource the dependency property as well as CLR property are necessary! Therefore they need to have the same name. If there aren't, then you will get an error.
If you set the property to the StaticResource or directly to the value or even do not use it in XAML, then you will be able to run the application.
DependencyProperty implementation is slightly weird.
The XAML compiler depends on the CLR property wrapper in order to compile, but at runtime bindings ignore it completely and just call GetValue/SetValue on the DP. Therefore the names should match.
There was a typo in the first example. The registered dependency property name needs to be the same as the CLR-backing property.
Declaring a DependencyProperty is a two-stage process:
Register the property so that the WPF DependencyProperty system can track it and notify when properties change, etc.
Setup a CLR property that gives developers an API to get and set values.
I hope this helps.

Access logical tree from inside a converter

I need to access the Logical Tree inside a converter. Actually this is inside a UserControl, which is (I think) only relevant insofar as this UserControl can live multiple times in my application.
I found a solution, but a rather crude one and my question is: Is there a better, more elegant solution.
Here is what I did. I added an arbitrary control ("Anchor") as a property to my converter. With that control I access the logical tree. In the example I grab the Tag property from the enclosing Grid and convert the value accordingly.
public class SomeConverter : IValueConverter
{
public System.Windows.Controls.Control Anchor { get; set; }
public object Convert(object value, Type t, object parameter, CultureInfo culture)
{
return toUpper() ? value.ToString().ToUpper() : value;
}
public object ConvertBack(object value, Type t, object parameter, CultureInfo culture)
{
return value;
}
private bool toUpper()
{
string tag = (Anchor.Parent as Grid).Tag as string;
return ! String.IsNullOrEmpty(tag);
}
}
So far so good. The real ugly part is how I assign the control to the property. I create an empty ContentControl and assign it to the converter definition. In order for the ContontControl to be in the logical tree I also need to instantiate is somewhere, which I do with Visibility=hidden. Here's the XAML:
<Window x:Class="WpfApp4__Various_Tests_.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp4__Various_Tests_"
Title="MainWindow" Height="100" Width="300">
<Window.Resources>
<ContentControl x:Key="anchor" Visibility="Hidden"/>
</Window.Resources>
<Grid Tag="toUpper">
<Grid.Resources>
<local:SomeConverter x:Key="SomeConverter" Anchor="{StaticResource anchor}"/>
</Grid.Resources>
<StaticResource ResourceKey="anchor" />
<TextBox
x:Name="textBox"
Text="{Binding SomeProperty, Converter={StaticResource SomeConverter}}"
/>
</Grid>
You can try using MultiBinding with IMultiValueConverter. There you can pass the target element using a binding.

Wpf Databound TextBlock not updating [duplicate]

This question already has an answer here:
Binding works without INotifyPropertyChanged, why?
(1 answer)
Closed 5 years ago.
I have a simple WPF window with a slider and two textblocks. As the slider moves it updates a data bound object. Now the first textblock updates while the second does not. Why?
You may say there is no INotifyPropertyChanged here. But then why is the first updating? I have pulled my hair enough. Please help.
My WPF app in all its glory is as follows.
<Window x:Class="DataTriggerDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataTriggerDemo"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Slider x:Name="MySlider" Margin="5" Minimum="0" Maximum="100"
Value="{Binding TheValue}"/>
<TextBlock Grid.Row="1" Text="{Binding TheValue}" />
<TextBlock Grid.Row="2" Text="{Binding TheValueTwice}" />
</Grid>
</Window>
And now the code behind.
using System.Windows;
namespace DataTriggerDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DataObject();
}
}
public class DataObject
{
private int _theValue;
public int TheValue
{
get { return _theValue; }
set {
_theValue = value;
TheValueTwice = _theValue * 2;
}
}
private int _theValueTwice;
public int TheValueTwice
{
get {
return _theValueTwice;
}
set {
_theValueTwice = value;
}
}
}
}
Actually you are encountering a another hidden aspect of WPF, that's it WPF's data binding engine will data bind to PropertyDescriptor instance which wraps the source property if the source object is a plain CLR object and doesn't implement INotifyPropertyChanged interface. And the data binding engine will try to subscribe to the property changed event through PropertyDescriptor.AddValueChanged() method. And when the target data bound element change the property values, data binding engine will call PropertyDescriptor.SetValue() method to transfer the changed value back to the source property, and it will simultaneously raise ValueChanged event to notify other subscribers (in this instance, the other subscribers will be the TextBlocks within the ListBox.
Please refer to: https://social.msdn.microsoft.com/Forums/vstudio/en-US/9365bb6a-b411-4967-9a03-ae2a810fb215/data-binding-without-inotifypropertychanged?forum=wpf

Referencing objects in XAML design data

How can I reference the same object twice (or more often) in a XAML design data file?
I tried to use {x:Reference}, but this does not seem to work.
Here is an example:
The combo box in the cells of the second column of the sample's data grid displays a list of "data types". The list of available data types comes from the Types property of the main window's view model (= the data context). The list of items in the grid comes from the Items property of the view model. Each item has a Name and a Type column, where Type references a data type object.
The sample grid looks like this:
Here is the XAML design data which should show the same grid contents in the Visual Studio designer (but it doesn't):
<?xml version="1.0" encoding="utf-8" ?>
<local:MainWindowViewModel
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridSample"
>
<local:MainWindowViewModel.Types>
<local:DataType Name="String" x:Name="String"/>
<local:DataType Name="Integer" x:Name="Integer"/>
</local:MainWindowViewModel.Types>
<local:MainWindowViewModel.Items>
<local:Item Name="Lorem" Type="{x:Reference String}"/>
<local:Item Name="Ipsum" Type="{x:Reference Integer}"/>
</local:MainWindowViewModel.Items>
</local:MainWindowViewModel>
Above, I am using {x:Reference String} to obtain a reference to the object that was created by <local:DataType Name="String" x:Name="String"/>.
In the Visual Studio designer, the list is empty, and the error message "Errors found in markup: ... DesignData.xaml" is displayed. In the editor for the design data XAML files, I get the error message "Service provider is missing the INameResolver service".
Is there any alternative to {x:Reference} which I could use in design data files to refer to an object?
For completeness, here are the remaining files of my sample:
MainWindow.xaml:
<Window x:Class="DataGridSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Title="Sample" Height="300" Width="400"
d:DataContext="{d:DesignData Source=DesignData.xaml}">
<Window.Resources>
<CollectionViewSource x:Key="types" Source="{Binding Types}"/>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="*"/>
<DataGridComboBoxColumn SelectedItemBinding="{Binding Type}"
ItemsSource="{Binding Source={StaticResource types}}"
DisplayMemberPath="Name"
Header="Type" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataGridSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
readonly MainWindowViewModel _viewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
}
}
MainWindowViewModel.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace DataGridSample
{
public class MainWindowViewModel
{
private readonly ObservableCollection<DataType> _dataTypes;
private readonly ObservableCollection<Item> _items;
public MainWindowViewModel()
{
DataType typeString = new DataType {Name = "String"};
DataType typeInteger = new DataType {Name = "Integer"};
_dataTypes = new ObservableCollection<DataType> {typeString, typeInteger};
_items = new ObservableCollection<Item>
{
new Item {Name = "Lorem", Type = typeString},
new Item {Name = "Ipsum", Type = typeInteger}
};
}
public ObservableCollection<DataType> Types
{
get
{
return _dataTypes;
}
}
public ObservableCollection<Item> Items
{
get
{
return _items;
}
}
}
public class DataType
{
public string Name { get; set; }
}
public class Item
{
public string Name { get; set; }
public DataType Type { get; set; }
}
}
Background on why x:Reference doesn't work .....
x:Reference is a XAML 2009 feature.
You can't use x:Reference in XAML markup that is compiled according to the MSDN docs.
http://msdn.microsoft.com/en-us/library/ee795380.aspx
It's designed for loose XAML...e.g. if you create a XAML Page (.xaml) and load it via Internet Explorer.
When you use DesignData the Designer effectively creates and compiles a new class whose shape and content is as described in your DesignData file.
There's no support for it in Visual Studio/Blend Designers.
http://www.infoq.com/news/2009/12/XAML-2009-Future
Here's a counter argument.
Here is the explanation from Adam Nathan's WPF 4 unleashed book: "The
x:Reference markup extension is often mistakenly associated with the
XAML2009 features that can only be used from loose XAML at the time
of this writing. Although x:Reference is a new feature in WPF 4, it
can be used from XAML2006 just fine as long as your project is
targeting version 4 or later of the .NET Framework. One glitch is
that the XAML designer in Visual Studio 2010 doesn�t properly handle
x:Reference, so it gives the following design-time error that you can
safely ignore: Service provider is missing the INameResolver service"
http://wpftutorial.net/XAML2009.html
Workaround solution ...

What do I need to do to make property element syntax work for my custom attached property in Silverlight?

I have a class like this:
public class Stretcher : Panel {
public static readonly DependencyProperty StretchAmountProp = DependencyProperty.RegisterAttached("StretchAmount", typeof(double), typeof(Stretcher), null);
public static void SetStretchAmount(DependencyObject obj, double amount)
{
FrameworkElement elem = obj as FrameworkElement;
elem.Width *= amount;
obj.SetValue(StretchAmountProp, amount);
}
}
I can set the stretch amount property in XAML using the attribute syntax:
<UserControl x:Class="ManagedAttachedProps.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:ManagedAttachedProps"
Width="400" Height="300">
<Rectangle Fill="Aqua" Width="100" Height="100" map:Stretch.StretchAmount="100" />
</UserControl>
and my rectangle is stretched, but I can't use property element syntax like this:
<UserControl x:Class="ManagedAttachedProps.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:ManagedAttachedProps"
Width="400" Height="300">
<Rectangle Fill="Aqua" Width="100" Height="100">
<map:Stretcher.StretchAmount>100</map:Stretcher.StretchAmount>
</Rectangle>
</UserControl>
with the property element syntax my set block seems to be totally ignored (I can even put invalid double values in there), and the SetStretchAmount method is never called.
I know it's possible to do something like this, because VisualStateManager does it. I've tried using types other than double and nothing seems to work.
I think I figured this out, although I'm not totally sure I understand the reason why it works.
In order to get your example to work I had to create a custom type called Stretch with a property called StretchAmount. Once I did that and put that inside the property element tags it worked. Otherwise it wasn't called.
public class Stretch
{
public double StretchAmount { get; set; }
}
And the property changed to..
public static readonly DependencyProperty StretchAmountProp = DependencyProperty.RegisterAttached("StretchAmount", typeof(Stretch), typeof(Stretcher), null);
public static void SetStretchAmount(DependencyObject obj, Stretch amount)
{
FrameworkElement elem = obj as FrameworkElement;
elem.Width *= amount.StretchAmount;
obj.SetValue(StretchAmountProp, amount);
}
To get this to work in the scenario where you aren't using a property element you would need to create a custom type converter to allow this to work.
Hope this helps, even though it doesn't explain the why which I'm still trying to understand.
BTW - for a real brain teaser, take a look at the VisualStateManager in reflector. The dependency property and the setter for VisualStateGroups are both internal.
So Bryant's solution works, it does require a slight modification to the XAML:
<Rectangle Fill="Aqua" Width="100" Height="100" x:Name="the_rect">
<map:Stretcher.StretchAmount>
<map:Stretch StretchAmount="100" />
</map:Stretcher.StretchAmount>
</Rectangle>

Resources