question

CharlesHe-MSFT avatar image
CharlesHe-MSFT asked ·

Button.Width Binding in User Control does not work

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!


You can try to add the following code:

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


Thanks.

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.