简体   繁体   English

在子对象的情况下,如何更改父控件的属性?

[英]How to alter property of a parent control, in a event of a child object?

<Style TargetType="controls:ModernVerticalMenu" >
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate  TargetType="controls:ModernVerticalMenu">
            <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="{TemplateBinding ListWidth}"/>
                                <ColumnDefinition Width="{TemplateBinding ListWidth}"/>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>


                            <Border Background="{DynamicResource background}" Height="{TemplateBinding Height}" BorderThickness="1" BorderBrush="{DynamicResource bordaSuperior}">
                                <!-- link list -->
                                <ListBox x:Name="LinkList" ItemsSource="{TemplateBinding Links}"  
                                         ScrollViewer.HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" >
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <Grid Height="50" Background="Transparent" Width="500">
                                                <Border Padding="10">
                                                    <Path x:Name="icon" Data="{Binding IconData}" Stretch="Fill" Fill="{DynamicResource Accent}" Width="20" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" />
                                                </Border>
                                                <TextBlock x:Name="texto" ToolTip="{Binding Tooltip}"  Text="{Binding DisplayName}" Margin="45,2,2,2" FontSize="{DynamicResource MediumFontSize}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center" HorizontalAlignment="Left" />
                                            </Grid>
                                            <DataTemplate.Triggers>
                                                <DataTrigger Binding="{Binding IconData}" Value="{x:Null}">
                                                    <Setter Property="Margin" TargetName="texto">
                                                        <Setter.Value>
                                                            <Thickness Bottom="2" Top="2" Left="10" Right="2"/> 
                                                        </Setter.Value>
                                                    </Setter>
                                                </DataTrigger>
                                                <Trigger Property="IsMouseOver" Value="true">
                                                    <Setter Property="Fill" TargetName="icon">
                                                        <Setter.Value>
                                                            <SolidColorBrush Color="#f2f2f2" />
                                                        </Setter.Value>
                                                    </Setter>
                                                </Trigger>

                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
     </Setter>
</Style>

I created a custom control and I am creating a template for a vertical menu, I wanna that in the event of MouseOver of the ListBox, I could set the value of proprierty of the controls:ModernVerticalMenu, any idea? 我创建了一个自定义控件,并为垂直菜单创建了一个模板,我想在ListBox的MouseOver事件中,我可以设置控件的属性值:ModernVerticalMenu,您知道吗?

When I write parent, is the ModerVerticalMenu and child is the ListBox from the ControlTemplate. 当我编写父级时,是ModerVerticalMenu,子级是ControlTemplate中的列表框。

The main idea is to somehow get access to parent object and change its property. 主要思想是以某种方式访问​​父对象并更改其属性。 In order to do this I create one attached property retrieving access to parent by TemplatedParent markup, second attached property stores value which is supposed to be applied in parent's property. 为此,我创建了一个附加属性,通过TemplatedParent标记检索对父级的访问,第二个附加属性存储应该应用于父级属性中的值。 This sample is not precise your problem but on the right track, I believe. 我相信,此样本不能精确地解决您的问题,但可以在正确的轨道上进行。 I want to change button Content, which is initially Empty. 我想更改按钮内容,该内容最初是空的。 First textblock reflects change in Button's Content to find out whether any occured. 第一个文本块反映了Button内容中的更改,以查找是否发生了任何更改。

<StackPanel>
    <TextBlock Text="{Binding ElementName=but, Path=Content}"/>
    <Button Name="but" Content="" Background="CadetBlue" BorderBrush="CadetBlue" BorderThickness="2">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <TextBox Margin="50" Text="xD" local:MyClass.Parent="{Binding RelativeSource={RelativeSource AncestorType=Button}}">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Setter Property="local:MyClass.Content" Value="{x:Null}"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True">
                                    <Setter Property="Foreground" Value="CadetBlue"/>
                                    <Setter Property="local:MyClass.Content" Value="tekst"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </ControlTemplate>
        </Button.Template>
    </Button>
</StackPanel>

Class with attached properties looks as follows 具有附加属性的类如下所示

public static class MyClass
{
    public static void SetParent(DependencyObject obj, Button val)
    {
        obj.SetValue(ParentProperty, val);
    }

    public static Button GetParent(DependencyObject obj)
    {
        return (Button)obj.GetValue(ParentProperty);
    }
    public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent", typeof(Button), typeof(MyClass), new PropertyMetadata(null, new PropertyChangedCallback(
        (x, y) =>
        {
            Debug.WriteLine(GetParent(x));
        })));

    public static void SetContent(DependencyObject obj, string val)
    {
        obj.SetValue(ContentProperty, val);
    }

    public static string GetContent(DependencyObject obj)
    {
        return (string)obj.GetValue(ContentProperty);
    }
    public static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterAttached("Content", typeof(string), typeof(MyClass), new PropertyMetadata(null, new PropertyChangedCallback(
        (x, y) =>
        {
            if (GetContent(x) != String.Empty)
                ((Button)GetParent(x)).Content = GetContent(x);
        })));
}

When mouse is over textbox then parent's property content changes to value of attached property set in setter 当鼠标悬停在文本框上时,父项的属性内容将更改为在setter中设置的附加属性的值

<Setter Property="local:MyClass.Content" Value="tekst"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过父控件中的事件为子控件设置动画? -可重复使用的方式 - How to animate child control by event in the parent control? - Reusable way 将子控件的点击事件传递给父控件 - Pass click event of child control to the parent control 从子控件引发事件到父控件 - Raising an event from child control to parent control 如何在背后的代码上设置控制子项的继承父属性 - How to set inherited parent property for a Control Child on Code Behind 如何捕获父控件中的子项生成的事件? - How can I catch an event generated by a child in a parent control? 如何在 AvaloniaUI 中将自定义事件从子 UserControl 引发到父控件 - How to raise a custom event from child UserControl to parent control in AvaloniaUI WPF - 家长如何知道子控件复选框的“已选中”事件? - WPF - How does Parent know of on “Checked” event for child control checkbox? 如何从子对象获取/更新父对象的属性 - How to get/update property of parent object from child object 如何使用自动混合在子属性中自动设置父对象 - How to automatically set the parent object in a child property with autofixture 如何通过子控件事件更改父控件的内容? - How can I change parent control's content though child control event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM