111,096
社区成员




<Rectangle x:Name="TrackPath" Width="200"/>
<TextBlock Margin="{Binding ElementName=TrackPath,Value=Width} 0 0 0"/>
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:WidthToMarginConvert x:Key="WidthToMargin"/>
</Window.Resources>
<Grid>
<Rectangle x:Name="TrackPath" Width="200" Fill="CadetBlue"/>
<TextBlock Text="Test" FontSize="15" FontWeight="Bold" Margin="{Binding ElementName=TrackPath, Path=Width, Converter={StaticResource WidthToMargin}}" />
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Data;
namespace WpfTest
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class WidthToMarginConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Double width = (Double)value;
return new Thickness(width, 0, 0, 0);
}
//没有用到
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
<TextBlock>
<TextBlock.Margin>
<Thickness Left="200"/>
</TextBlock.Margin>
</TextBlock>
可以这么设置该属性的