我想使用4個文本框設置用戶控件邊框的邊框厚度,但是我無法使它工作。WPF:4個文本框和1個邊框之間的雙向綁定。邊距
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正確顯示厚度的左側部分,但編輯文本框不會導致邊界左側呈現方式發生變化。奇怪的是,我爲Thickness.Left在TextBox中設置的值仍然存在,所以看起來該值確實已設置,但渲染未更新。 在示例代碼中,更改TextBox中的值,然後調整窗口大小,顯示左側的邊框佔用更多空間,但此空間爲空白。
有誰知道如何去解決這個問題?