wpf WPF通过元素的名称将属性绑定到另一个元素属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19606342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Bind property to another element property by the element's name
提问by Ron
I have the following xaml:
我有以下 xaml:
<Grid KeyboardNavigation.TabNavigation="Local" SnapsToDevicePixels="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Background="Transparent" BorderThickness="0,0,0,2" BorderBrush="{StaticResource TabPanelBorderBrush}">
<DockPanel LastChildFill="True">
<Button x:Name="LeftButton" Content="3" DockPanel.Dock="Left" Style="{DynamicResource TabControlButton}"></Button>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
<Button x:Name="RightButton" Content="4" Style="{DynamicResource TabControlButton}"></Button>
<Button x:Name="TabItemsList" Content="L" FontFamily="Segoe UI" Style="{DynamicResource TabControlButton}"></Button>
<Button x:Name="AddTabItem" Content="+" FontFamily="Segoe UI" Style="{DynamicResource TabControlButton}"></Button>
</StackPanel>
<ScrollViewer x:Name="ScrollViewer" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden">
<TabPanel x:Name="HeaderPanel" IsItemsHost="True" Panel.ZIndex="1" KeyboardNavigation.TabIndex="1"/>
</ScrollViewer>
</DockPanel>
</Border>
<Border Grid.Row="1" Background="{StaticResource TabControlBackground}"/>
<ContentPresenter Grid.Row="1" Name="PART_SelectedContentHost" ContentSource="SelectedContent"/>
</Grid>
<ListBox x:Name="TabItemsListBox" Width="200" Height="200" HorizontalAlignment="Right" VerticalAlignment="Top" Visibility="Collapsed">
<ListBox.Margin>
<Thickness Left="0" Top="{Binding to TabItemsList height}" Right="0" Bottom="20"/>
</ListBox.Margin>
</ListBox>
</Grid>
I want to bind the ListBox's top Thickness(TabItemsListBox) to the TabItemsList's Height.
How can I do that? tried:
我想将ListBox的顶部Thickness(TabItemsListBox)绑定到TabItemsList的Height。我怎样才能做到这一点?试过:
{Binding ElementName=TabItemsList, Path=Height}
but my program crushes when I run it
但是当我运行它时我的程序崩溃了
回答by iulian3000
I hope it works, now I use multibinding. With this you must provide 4 bindings or it will fail, or you can do your tests to prevent any errors in the converter.
我希望它有效,现在我使用多重绑定。有了这个,您必须提供 4 个绑定,否则它将失败,或者您可以进行测试以防止转换器中出现任何错误。
Xaml:
Xml:
<ListBox x:Name="TabItemsListBox"
Width="50"
Height="50">
<ListBox.Margin>
<MultiBinding Converter="{StaticResource Converter}">
<MultiBinding.Bindings>
<Binding ElementName="TabItemsListBox"
Path="ActualHeight" />
<Binding ElementName="TabItemsListBox"
Path="ActualHeight" />
<Binding ElementName="TabItemsListBox"
Path="ActualHeight" />
<Binding ElementName="TabItemsListBox"
Path="ActualHeight" />
</MultiBinding.Bindings>
</MultiBinding>
</ListBox.Margin>
</ListBox>
Converter:
转换器:
public class DoubleToMarginConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var left = (double)values[0];
var top = (double)values[1];
var right = (double)values[2];
var bottom = (double)values[3];
return new Thickness(left, top, right, bottom);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
What is bothering me the most is that I dont get intellisense with multibinding. I'm a newb too :)
最让我烦恼的是我没有通过多重绑定获得智能感知。我也是新手 :)
回答by iulian3000
<ListBox x:Name="TabItemsListBox"
Width="200"
Height="200"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Visibility="Visible"
Margin="{Binding ElementName=TabItemsListBox, Path=ActualHeight , Converter={StaticResource Converter}}"
>
<ListBoxItem>
<Button Content="Button" />
</ListBoxItem>
</ListBox>
and the converter
和转换器
public class DoubleToTopMarginConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var top = (double)value;
return new Thickness(0, top, 0, 20);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
This post sais that it works, binding to Bottom margin, but not for me. https://stackoverflow.com/a/19454618/1775703
这篇文章说它有效,绑定到底部边距,但不适合我。https://stackoverflow.com/a/19454618/1775703

