I am trying to make the width of a column in a Grid element variable. For this I have a DependencyProperty “ItemWidth” and bind the Width-element from the Button to this DP. Because of the TwoWay-Binding, I need a converter that converts doubles to DataGridLength.
My MainWindow.xaml looks like this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:utils="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.Resources>
<utils:ColumnWidthConverter x:Key="columnWidthConverter"/>
</Grid.Resources>
<Button Grid.Row="0" Grid.Column="0" Width="{Binding Path=ItemWidth, Mode=TwoWay, Converter={StaticResource columnWidthConverter}}" Click="Shorter_Click">shorter</Button>
<Button Grid.Row="0" Grid.Column="1" Width="{Binding Path=ItemWidth, Mode=TwoWay, Converter={StaticResource columnWidthConverter}}" Click="Longer_Click">longer</Button>
</Grid>
</Window>
The ColumnWidthConverter.cs is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
namespace WpfApplication1
{
class ColumnWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
else
{
DataGridLengthConverter cv = new DataGridLengthConverter();
object result = cv.ConvertFrom(value);
return result;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
else
{
DataGridLengthConverter cv = new DataGridLengthConverter();
return cv.ConvertTo(value, typeof(double));
}
}
}
}
And the MainWindow.xaml.cs looks like this:
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 WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public double ItemWidth
{
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public static readonly DependencyProperty ItemWidthProperty =
DependencyProperty.Register("ItemWidth", typeof(double), typeof(MainWindow), new UIPropertyMetadata(0.0));
private void Shorter_Click(object sender, RoutedEventArgs e)
{
this.ItemWidth -= 100;
}
private void Longer_Click(object sender, RoutedEventArgs e)
{
this.ItemWidth += 100;
}
}
}
So the Width of the Buttons should change when I click one of the Buttons. But this does not happen. Can you tell me why that is and some sort of solution?
You set no source in the binding, hence it is relative to the
DataContext, which you do not appear to have set anywhere, if you add it that should work. e.g.You might want to (i.e. you definitely should) look into debugging data bindings if you are not familiar with it.
You also bind properties which have no need for the converter, in fact the converter will probably cause problems here, did you not mean to bind the
ColumnDefinition.Width?