WPF:4 个文本框和 1 个 Border.Margin 之间的 TwoWay 绑定
我想使用 4 个文本框设置 UserControl 边框的 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>
需要转换器来解析文本框中输入的字符串:
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 中为 Thickness.Left 设置的值仍然存在,因此似乎该值确实已设置,但渲染未更新。 在示例代码中,更改 TextBox 中的值,然后调整 Window 的大小,显示左侧边框确实占用了额外的空间,但该空间是空白的。
有谁知道如何解决这个问题?
I want to set the BorderThickness of a Border of a UserControl using 4 TextBoxes, but I can't get it to work.
XAML code demonstrating the problem (only this code in combination with the converter is needed):
<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>
A converter is needed to parse the string input in the 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;
}
}
The TextBox correctly displays the Left part of the Thickness, but editing the TextBox does not result in a change in the way the left side of the Border is rendered. Oddly, the value that I set in the TextBox for Thickness.Left persists, so it seems that the value does get set, but the rendering isn't updated.
In the example code, changing the value in the TextBox, then resizing the Window, shows that the border on the left does take up additional space, but this space is blank.
Does anyone know how to go about and fixing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不会动态更新屏幕上的元素,因为没有任何信息告诉元素其
BorderThickness
属性中的字段已更改。您需要通知元素其BorderThickness
已更改,这只能通过直接将依赖属性设置为新值来实现 - 例如,将其作为绑定到执行以下操作的对象的目标:更改通知。为此创建视图模型是一件痛苦的事情,但是一旦你这样做了,就完成了。
窗口:
视图模型:
It's not dynamically updating the element on the screen because nothing has told the element that a field in its
BorderThickness
property has changed. You need to notify the element that itsBorderThickness
has changed, which you can only do by directly setting the dependency property to a new value - say, by making it the target of a binding to an object that does change notification.It's something of a pain to make a view model for this, but once you do, it's done.
The window:
The view model:
我相信这将为您指明正确的方向:读到一半,它有两种方法可以实现这一点,一种使用转换器,一种不使用转换器。
http:// /10rem.net/blog/2010/05/08/break-apart-the-margin-property-in-xaml-for-better-binding
I believe this will point you in the right direction: Read halfway down it has 2 ways of approaching this, one with a converter and one without.
http://10rem.net/blog/2010/05/08/breaking-apart-the-margin-property-in-xaml-for-better-binding
对我来说最简单的解决方案是只监听 TextBox 的 TextChanged 事件,并替换后面代码中的 BorderThickness 。
MainWindow.xaml:
MainWindow.xaml.cs,在构造函数中:
现在这对我有用,罗伯特·罗斯尼的解决方案更好。
The simplest solution for me turns out to be to just listen to the TextChanged event of the TextBox, and replace the BorderThickness in code behind.
MainWindow.xaml:
MainWindow.xaml.cs, in the constructor:
Right now this works for me, Robert Rossney's solution is better.