Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7192999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:04:48+00:00 2026-05-28T20:04:48+00:00

I am trying to make the width of a column in a Grid element

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:04:48+00:00Added an answer on May 28, 2026 at 8:04 pm

    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.

    <Window DataContext="{Binding RelativeSource={RelativeSource Self}}" ...
    

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF Application where I am trying to make a close button
Let's say i have a Domain Model that im trying to make compatible with
I am trying to make a 2 column layout, apparently the bane of CSS.
I'm trying to make a 2D grid of JTextFields to represent a level map.
i am trying to make a grid in such a way that it is
I'm working with a kendo grid and I'm trying to make Name field a
I have a TextView that I am trying to make display a 2d character-based
i have a listbox and i am trying to make the scrolling work without
I am trying to make a Xml Image Menu that on button click goes
I'm trying to make a 2x2 grid, which fills up the entire window in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.