Button.Width Binding in User Control does not work

Charles He-MSFT 96 Reputation points Microsoft Employee
2020-02-25T03:08:39.24+00:00

Source thread: Button.Width Binding in User Control, answered by Alex Li-MSFT.

I have a user control which contains a Button, and I want to bind the width property of the Button to another field of the user control.

Here is my .cs code:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
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 Projekt.CustomControls  
{  
    public partial class CustomControl : UserControl, INotifyPropertyChanged  
    {  
        protected double buttonWidth = 50;  
        public double ButtonWidth  
        {  
            get  
            {  
                return this.buttonWidth ;  
            }  
            set  
            {  
                this.buttonWidth  = value;  
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ButtonWidth"));  
            }  
        }  
  
        public event PropertyChangedEventHandler PropertyChanged;  
  
        public CustomControl ()  
        {  
            InitializeComponent();  
        }  
    }  
}  

And here is the XAML code:

<UserControl x:Class="Project.CustomControls.CustomControl"  
             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:Project.CustomControls"  
             mc:Ignorable="d"   
             d:DesignHeight="450" d:DesignWidth="800">  
    <UserControl.Resources>  
        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">  
            <Setter Property="Button.Width" Value="{Binding ButtonWidth, UpdateSourceTrigger=PropertyChanged}">  
            </Setter>  
        </Style>  
    </UserControl.Resources>  
    <Grid>  
        <Grid.ColumnDefinitions>  
            <ColumnDefinition Width="1*"></ColumnDefinition>  
            <ColumnDefinition Width="Auto"></ColumnDefinition>  
        </Grid.ColumnDefinitions>  
        <Button x:Name="CustomControl_Button" Grid.Column="1" Style="{DynamicResource ButtonStyle}">  
        </Button>  
    </Grid>  
</UserControl>  

I want to bind Button's width to the buttonWidth field. Any suggestion?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-02-25T03:14:05.587+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    You can try to add the following code:

    public CustomControl ()  
            {  
                InitializeComponent();  
                this.DataContnt=this;  
            }  
    

    Thanks.


0 additional answers

Sort by: Most helpful