[英]Bind a Grid Row Height to a Dependency Property of User Control
I think I'm missing something here, and it's probably something simple - but here is my issue. 我想我在这里遗漏了一些东西,可能很简单-但这是我的问题。
I have created a WPF UserControl which consists of a multi-row grid, each row containing a button. 我创建了一个WPF UserControl,它由一个多行网格组成,每行包含一个按钮。 On this UserControl, I have implemented a number of dependency properties, the intent of which is to control the row height of the corresponding button.
在此UserControl上,我实现了许多依赖项属性,其目的是控制相应按钮的行高。 By setting the value to
true
, the intent is that the row shows up (height 70* instead of 0); 通过将该值设置为
true
,目的是显示该行(高度70 *而不是0); however, currently the row height is not getting reset to the proper value of 70* . 但是,当前行高尚未重置为70 *的适当值 。 Any idea what I did wrong?
知道我做错了什么吗? I can't figure out how to debug the ValueConverter, as a breakpoint set in it is never hit.
我无法弄清楚如何调试ValueConverter,因为永远不会命中其中设置的断点。 Probably the binding logic is incorrect, but I don't know what else to put in it.
绑定逻辑可能不正确,但是我不知道该添加什么。
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));
So, to go from bool
to GridLength
, I have an IValueConverter
: 因此,要从
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();
}
}
And, in the XAML, I'm trying to do the binding: 而且,在XAML中,我正在尝试进行绑定:
<RowDefinition Height="{Binding ElementName=ShowCancelButton, Converter={StaticResource RowVisibilityConverter}}"/>
You should be able to do this entirely in xaml, from what I understood you are doing. 据我了解,您应该能够完全在xaml中完成此操作。
Just name the root of your usercontrol and bind with the ElementName of that and the path of your DependencyProperty. 只需命名用户控件的根目录,并与该元素的ElementName和DependencyProperty的路径绑定即可。
Toggling of the DependencyProperty is then up to you to decide if it is a togglebutton or mouse/keyboard event. 然后由DependencyProperty切换,由您决定是切换按钮还是鼠标/键盘事件。
<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>
Change your Binding
as following: 更改
Binding
,如下所示:
<RowDefinition Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Path=ShowCancelButton, Converter={StaticResource RowVisibilityConverter}}"/>
Above expression
is on the basis of information you have given, UserControl
has DependencyProperty
named ShowCancelButton
. 上面的
expression
基于您提供的信息, UserControl
具有名为ShowCancelButton
DependencyProperty
。 So you need to do Ancestor Binding If you want to access a Parent's property from Child Elements
. 因此,如果要从
Child Elements
访问Parent的属性,则需要进行祖先绑定。
PS: Change UserControl
with your actual type of parent Control
. PS:用您的实际父
Control
类型更改UserControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.