【问题标题】:WPF Bind property to another element property by the element's nameWPF 通过元素名称将属性绑定到另一个元素属性
【发布时间】:2013-11-05 13:05:52
【问题描述】:

我有以下 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>

我想将ListBox 的顶部Thickness (TabItemsListBox) 绑定到TabItemsListHeight。 我怎样才能做到这一点?试过了:

{Binding ElementName=TabItemsList, Path=Height}

但是当我运行它时我的程序崩溃了

【问题讨论】:

  • 尝试实际高度而不是高度
  • Top 是厚度类型,这就是它压碎的原因,并且您将双精度绑定到厚度。也许在转换器的帮助下,您可以绑定到 Top
  • 根据您的评论编辑了我的帖子

标签: wpf binding


【解决方案1】:

我希望它有效,现在我使用多重绑定。有了这个,您必须提供 4 个绑定,否则它将失败,或者您可以进行测试以防止转换器出现任何错误。

Xaml:

 <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>

转换器:

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();
    }
}

最困扰我的是我无法通过多重绑定获得智能感知。我也是新手:)

【讨论】:

  • 不,谢谢你,因为我从来没有使用过多重绑定。现在我现在也是如何工作的:)
【解决方案2】:
    <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>

和转换器

 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();
     }
  }

这篇文章说它有效,绑定到底部边距,但不适用于我。 https://stackoverflow.com/a/19454618/1775703

【讨论】:

  • 1.没有转换器就不能工作。 2. 我修改了你的答案以匹配我的程序并且它有效,但我有一个问题。您返回厚度并将其绑定到边距。我只想绑定 Margin.Top,因为与绑定 Margin.Top 的方式相同,我要将 Margin.Right 绑定到另一个元素...
  • 我还删除了我在原始帖子中的编辑,因为我做错了 - 将 double 转换为我不需要的 int...
  • 问题是Left,Top..不是依赖属性,所以我不能直接绑定到它。
  • 我明白了,有没有可能实现我想要的? (将 top 绑定到一个元素的属性和 right 到另一个元素的属性)如果是,你能告诉我怎么做吗?因为我了解您为实现顶级绑定所做的工作,但不知道如何将其与另一个绑定链接。我最终想要的是Top将绑定到按钮的ActualHeight,而right将绑定到按钮的ActualWidth
猜你喜欢
  • 2018-06-04
  • 1970-01-01
  • 2023-04-04
  • 2018-10-21
  • 2018-05-14
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 2017-07-20
相关资源
最近更新 更多