我有这个:
<TabControl Margin="0,24,0">...</TabControl>
我只想绑定TabControl的“顶部”部分,直观地,我会这样做:
<TabControl Margin="0,{Binding ElementName=TheMenu,Path=Height},0"> ... </TabControl>
我该怎么做 ?
解决方法
你试过使用这样的转换器吗?
在VB.Net
Public Class MarginConverter Implements IValueConverter Public Function Convert(ByVal value As Object,ByVal targetType As System.Type,ByVal parameter As Object,ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert Return New Thickness(0,CDbl(value),0) End Function Public Function ConvertBack(ByVal value As Object,ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Return Nothing End Function End Class
或者在C#
public class MarginConverter : IValueConverter { public object Convert(object value,System.Type targetType,object parameter,System.Globalization.CultureInfo culture) { return new Thickness(0,System.Convert.ToDouble(value),0); } public object ConvertBack(object value,System.Globalization.CultureInfo culture) { return null; } }
XAML
<Window.Resources> <local:MarginConverter x:Key="marginConverter"></local:MarginConverter> </Window.Resources> <Grid> <StackPanel> <Slider Name="Slider1"></Slider> <TabControl Name="TabControl" Margin="{Binding ElementName=Slider1,Path=Value,Converter={StaticResource marginConverter}}"> <Button>Some content</Button> </TabControl> </StackPanel> </Grid>
编辑:
使用MultiConverter
还可以在运行时获取所有四个值,并使用MultiValueConverter。厚度对象的顶部属性不是依赖对象,因此您不能定义对它的绑定(除非您的源不是依赖对象)。
XAML
<Window.Resources> <local:MarginConverter x:Key="marginConverter"></local:MarginConverter> <local:MultiMarginConverter x:Key="multiMarginConverter"></local:MultiMarginConverter> </Window.Resources> <Grid> <StackPanel> <Slider Name="Slider1"></Slider> <Slider Name="Slider2"></Slider> <Slider Name="Slider3"></Slider> <Slider Name="Slider4"></Slider> <TabControl Name="TabControl"> <TabControl.Margin> <MultiBinding Converter="{StaticResource multiMarginConverter}"> <Binding ElementName="Slider1" Path="Value"></Binding> <Binding ElementName="Slider2" Path="Value"></Binding> <Binding ElementName="Slider3" Path="Value"></Binding> <Binding ElementName="Slider4" Path="Value"></Binding> </MultiBinding> </TabControl.Margin> <Button>Some content</Button> </TabControl> </StackPanel> </Grid>
…和c#
class MultiMarginConverter : IMultiValueConverter { public object Convert(object[] values,System.Globalization.CultureInfo culture) { return new Thickness(System.Convert.ToDouble(values[0]),System.Convert.ToDouble(values[1]),System.Convert.ToDouble(values[2]),System.Convert.ToDouble(values[3])); } public object[] ConvertBack(object value,System.Type[] targetType,System.Globalization.CultureInfo culture) { return null; } }
编辑(2)反向绑定:
我不知道这是否会让你快乐。在我谦卑的意见,我会尽量避免这一点,但确定…如果你的来源是一个依赖属性,你可以绑定到边际:
<Slider Name="Slider5" Minimum="-99" Maximum="0" Value="{Binding ElementName=TabControl,Path=Margin.Top,Mode=OneWayToSource}"></Slider>
但我有一些效果与此。诀窍是,你不绑定的TabControl的Margin的一部分为“其他”,但绑定“其他”到您的TabControl的边距,并指定绑定模式OneWayToSource。