【问题标题】:WPF: TwoWay binding between 4 TextBoxes and 1 Border.MarginWPF:4个TextBoxes和1个Border.Margin之间的双向绑定
【发布时间】:2023-03-03 23:06:02
【问题描述】:

我想使用 4 个文本框设置用户控件的边框的 BorderThickness,但我无法让它工作。

演示问题的 XAML 代码(仅需要此代码与转换器结合使用):

<Window 
    x:Class="BorderThicknessBindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:BorderThicknessBindingTest="clr-namespace:BorderThicknessBindingTest" 
    Height="300" Width="500">
    <Window.Resources>
        <BorderThicknessBindingTest:ThicknessConverter x:Key="ThicknessConverter"/>
    </Window.Resources>

    <Grid Margin="10">
        <Border 
            x:Name="MyBorder"
            BorderBrush="Black" 
            Background="AliceBlue"
            BorderThickness="3"/>
        <TextBox 
            HorizontalAlignment="Center" VerticalAlignment="Center"
            Text="{Binding Path=BorderThickness.Left, ElementName=MyBorder, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ThicknessConverter}}"/>
    </Grid>
</Window>

需要一个转换器来解析TextBox中输入的字符串:

public class ThicknessConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value; // don't need to do anything here
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        double d;
        Double.TryParse((string) value, out d); // Thickness.Left doesn't take a string
        return d;
    }
}

TextBox 正确显示了厚度的左侧部分,但编辑 TextBox 不会导致边框左侧的呈现方式发生变化。奇怪的是,我在 TextBox 中为 Thickness.Left 设置的值仍然存在,因此该值似乎已设置,但渲染未更新。 在示例代码中,更改 TextBox 中的值,然后调整 Window 的大小,显示左侧边框确实占用了额外的空间,但这个空间是空白的。

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: wpf data-binding border margin thickness


    【解决方案1】:

    它不会动态更新屏幕上的元素,因为没有任何东西告诉元素其BorderThickness 属性中的字段已更改。您需要通知元素其BorderThickness 已更改,您只能通过直接将依赖属性设置为新值来做到这一点 - 例如,将其作为绑定到确实更改通知的对象的目标。

    为此制作视图模型有些痛苦,但一旦完成,就完成了。

    窗口:

    <Window x:Class="ThicknessDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=System" xmlns:ThicknessDemo="clr-namespace:ThicknessDemo" Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ThicknessDemo:ThicknessViewModel x:Key="thickness" />
        </Window.Resources>
            <DockPanel DataContext="{StaticResource thickness}">
                <Border DockPanel.Dock="Top"
                        Width="100"
                        Height="50"
                        Margin="5"
                        BorderBrush="Blue"
                        BorderThickness="{Binding Thickness}" />
                <TextBox DockPanel.Dock="Top"
                            Text="{Binding Left, Mode=TwoWay}" />
                <TextBox DockPanel.Dock="Top"
                            Text="{Binding Right, Mode=TwoWay}" />
                <TextBox DockPanel.Dock="Top"
                            Text="{Binding Top, Mode=TwoWay}" />
                <TextBox DockPanel.Dock="Top"
                            Text="{Binding Bottom, Mode=TwoWay}" />
                <TextBlock DockPanel.Dock="Top" />
            </DockPanel>
    </Window>
    

    视图模型:

    public class ThicknessViewModel : INotifyPropertyChanged
    {
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler h = PropertyChanged;
            if (h != null)
            {
                h(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ThicknessViewModel()
        {
            _Thickness = new Thickness(1, 1, 1, 1);
        }
    
        private Thickness _Thickness;
    
        public Thickness Thickness { get { return _Thickness; } set { _Thickness = value;} }
    
        public double Left
        {
            get { return _Thickness.Left; }
            set
            {
                _Thickness.Left = value;
                OnPropertyChanged("Thickness");
            }
        }
    
        public double Right
        {
            get { return _Thickness.Right; }
            set
            {
                _Thickness.Right = value;
                OnPropertyChanged("Thickness");
            }
        }
    
        public double Top
        {
            get { return _Thickness.Top; }
            set
            {
                _Thickness.Top = value;
                OnPropertyChanged("Thickness");
            }
        }
    
        public double Bottom
        {
            get { return _Thickness.Bottom; }
            set
            {
                _Thickness.Bottom = value;
                OnPropertyChanged("Thickness");
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我相信这会为您指明正确的方向:读到一半,有两种方法可以解决这个问题,一种使用转换器,另一种没有。

      http://10rem.net/blog/2010/05/08/breaking-apart-the-margin-property-in-xaml-for-better-binding

      【讨论】:

        【解决方案3】:

        对我来说最简单的解决方案是只听 TextBox 的 TextChanged 事件,并在后面的代码中替换 BorderThickness。

        MainWindow.xaml:

        <Window 
            x:Class="BorderThicknessBindingTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:BorderThicknessBindingTest="clr-namespace:BorderThicknessBindingTest" 
            Height="300" Width="500">
        
            <Grid Margin="10">
                <Border 
                    x:Name="MyBorder"
                    BorderBrush="Black" 
                    Background="AliceBlue"
                    BorderThickness="3"/>
                <TextBox 
                    x:Name="MyTextBox"
                    HorizontalAlignment="Center" VerticalAlignment="Center"
                    Text="{Binding Path=BorderThickness.Left, ElementName=MyBorder, Mode=OneWay}"/>
            </Grid>
        </Window>
        

        MainWindow.xaml.cs,在构造函数中:

        MyTextBox.TextChanged += (sender, e) =>
        {
            double d;
            if (!double.TryParse(MyTextBox.Text, out d)) return;
            var t = MyBorder.BorderThickness;
            t.Left = d;
            MyBorder.BorderThickness = t;
        };
        

        目前这对我有用,Robert Rossney 的解决方案更好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-24
          • 1970-01-01
          • 2015-05-21
          • 2011-05-20
          • 1970-01-01
          相关资源
          最近更新 更多