【问题标题】:Binding not working on a single property of an object (while other properties are working)绑定对对象的单个属性不起作用(而其他属性正在起作用)
【发布时间】:2015-10-08 12:15:23
【问题描述】:

我正在使用以下代码制作一个可配置的 WPF 输入对话框:


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; }
}

当我调用它时,每个绑定都按预期工作,但只有一个 Width 属性。当我调试时,宽度属性值为 430,但框架本身的宽度比这大得多。令人困惑的部分是绑定的其余部分正在工作。为什么会这样?

【问题讨论】:

    标签: c# wpf xaml data-binding bind


    【解决方案1】:

    您可以通过将Width 的绑定模式设置为TwoWay 来解决此问题:

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

    您不妨考虑实现INotifyPropertyChanged 接口,以便在这些属性发生任何更改时自动通知 UI:

    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));
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2011-02-15
    • 2020-11-26
    • 2018-09-16
    • 2023-03-12
    • 2017-03-27
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多