Gabriel Duarte
Gabriel Duarte

Reputation: 1003

Binding not working on a single property of an object (while other properties are working)

I'm making a configurable WPF input dialog with the following code:


InputMessageBox.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    xmlns:local="clr-namespace:MediaManager.Forms" x:Class="MediaManager.Forms.InputMessageBox"
    Title="{Binding Title}" Height="{Binding Height}"  Width="{Binding Width}">
<Window.Background>
    <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />
</Window.Background>
<Grid>
    <xctk:WatermarkTextBox Watermark="{Binding Message}" Margin="10" VerticalAlignment="Top" TabIndex="0" />
    <Button Content="{Binding CancelButtonText}" Width="{Binding ButtonWidth}" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Bottom" IsCancel="True" TabIndex="2" />
    <Button Content="{Binding OkButtonText}" Width="{Binding ButtonWidth}" Margin="{Binding MarginOkButton}" HorizontalAlignment="Right" VerticalAlignment="Bottom" IsDefault="True" TabIndex="1" />
</Grid>


InputMessageBox.xaml.cs

public partial class InputMessageBox : Window
{
    public InputMessageBox(inputType inputType)
    {
        InitializeComponent();

        switch (inputType)
        {
            case inputType.AdicionarConteudo:
                {
                    Properties = new InputMessageBoxProperties()
                    {
                        ButtonWidth = 75,
                        CancelButtonText = "Cancelar",
                        Height = 108,
                        Message = "Digite o nome do conteudo a ser pesquisado...",
                        OkButtonText = "Pesquisar",
                        Title = string.Format("Pesquisar - {0}", Settings.Default.AppName),
                        Width = 430
                    };
                    break;
                }
            default:
                break;
        }

        DataContext = Properties;
    }

    public InputMessageBoxProperties Properties { get; set; }
}

InputMessageBoxProperties.cs

public class InputMessageBoxProperties
{
    public int ButtonWidth { get; set; }

    public string CancelButtonText { get; set; }

    public int Height { get; set; }

    public string InputText { get; set; }

    public Thickness MarginOkButton { get { return new Thickness(10, 10, ButtonWidth + 15, 10); } }

    public string Message { get; set; }

    public string OkButtonText { get; set; }

    public string Title { get; set; }

    public int Width { get; set; }
}

When i call it, every binding is working as expected but one, the Width property. When i debug, the width property value is 430, but the width of the frame itself is a lot bigger than that. The confusing part is that the rest of the binding are working. Why is it happening?

Upvotes: 1

Views: 80

Answers (1)

SamTh3D3v
SamTh3D3v

Reputation: 9944

You can fix that by setting the Width's Binding Mode to TwoWay :

 Width="{Binding Width,Mode=TwoWay}"

You might as well consider implementing the INotifyPropertyChanged interface, so that the UI will automatically be notified in case any changes occurs in those properties :

public class InputMessageBoxProperties:INotifyPropertyChanged
{

    private int _width  ;     
    public int Width
    {
        get
        {
            return _width;
        }

        set
        {
            if (_width == value)
            {
                return;
            }

            _width = value;
            OnPropertyChanged();
        }
    }
  // add the other properties following the same pattern 

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

Upvotes: 2

Related Questions