将网格行高度绑定到用户控件的依赖属性

时间:2016-04-18 03:17:59

标签: c# wpf xaml data-binding

我想我在这里遗漏了一些东西,这可能很简单 - 但这是我的问题。

我创建了一个WPF UserControl,它由一个多行网格组成,每行包含一个按钮。在这个UserControl上,我实现了许多依赖属性,其目的是控制相应按钮的行高。 通过将值设置为true,意图是显示行(高度70 *而不是0);但是,目前行高未重置为70 * 的正确值。知道我做错了什么吗?我无法弄清楚如何调试ValueConverter,因为它中设置的断点永远不会被命中。可能绑定逻辑不正确,但我不知道还有什么内容。

public bool ShowCancelButton
{
   get { return (bool)GetValue(ShowCancelButtonProperty); }
   set { SetValue(ShowCancelButtonProperty, value); }
}

// Using a DependencyProperty as the backing store for ShowCancelButton.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ShowCancelButtonProperty =
   DependencyProperty.Register("ShowCancelButton", typeof(bool), typeof(myClass), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

所以,从bool转到GridLength,我有一个IValueConverter

public class RowVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //This should return a grid row height value of either 0 (for hidden) or 70* (for visible)
        if (bool.Parse(value.ToString()))
            return new GridLength(70, GridUnitType.Star);

        return 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

而且,在XAML中,我正在尝试进行绑定:

<RowDefinition Height="{Binding ElementName=ShowCancelButton, Converter={StaticResource RowVisibilityConverter}}"/>

2 个答案:

答案 0 :(得分:2)

根据我的理解,您应该能够完全按照xaml这样做。

只需命名usercontrol的根目录,然后使用它的ElementName和DependencyProperty的路径绑定。

然后切换DependencyProperty以决定它是togglebutton还是鼠标/键盘事件。

<UserControl x:Name="MyGridControlRoot" x:Class="WpfApplication2.MyGridControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">

    <UserControl.Resources>
        <Style x:Key="RowStyle" TargetType="{x:Type RowDefinition}">
            <Setter Property="Height" Value="0"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyGridControlRoot, Path=ShowCancelButton}" Value="True">
                    <Setter Property="Height" Value="70*"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <StackPanel>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Style="{StaticResource RowStyle}"/>
                <RowDefinition Style="{StaticResource RowStyle}"/>
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Button/>
            </Grid>
            <Grid Grid.Row="1">
                <Button/>
            </Grid>
        </Grid>

        <ToggleButton x:Name="ToggleHeightButton" IsChecked="{Binding ElementName=MyGridControlRoot, Path=ShowCancelButton}"/>
    </StackPanel>

</UserControl>

答案 1 :(得分:1)

按以下方式更改Binding

<RowDefinition Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Path=ShowCancelButton, Converter={StaticResource RowVisibilityConverter}}"/>

以上expression基于您提供的信息,UserControl DependencyProperty名为ShowCancelButton。所以你需要做Ancestor绑定如果你想从Child Elements访问Parent的属性。

PS: 使用您实际的父级UserControl更改Control