![](/img/trans.png)
[英]Dependency property of user control doesn't bind to a property of ViewModel
[英]Bind a Grid Row Height to a Dependency Property of User Control
我想我在這里遺漏了一些東西,可能很簡單-但這是我的問題。
我創建了一個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}}"/>
據我了解,您應該能夠完全在xaml中完成此操作。
只需命名用戶控件的根目錄,並與該元素的ElementName和DependencyProperty的路徑綁定即可。
然后由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>
更改Binding
,如下所示:
<RowDefinition Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Path=ShowCancelButton, Converter={StaticResource RowVisibilityConverter}}"/>
上面的expression
基於您提供的信息, UserControl
具有名為ShowCancelButton
DependencyProperty
。 因此,如果要從Child Elements
訪問Parent的屬性,則需要進行祖先綁定。
PS:用您的實際父Control
類型更改UserControl
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.