Поведение ChangePropertyAction не работает в Popup с пользовательским элементом управления - PullRequest
0 голосов
/ 04 мая 2020

Привет, у меня есть следующее окно, у него есть кнопка, и когда я нажимаю на нее, она показывает всплывающее окно, содержащее некоторый текст. Когда всплывающее окно закрыто, поведение очищает текст внутри всплывающего окна

<Window
x:Class="WpfTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviours="http://schemas.microsoft.com/xaml/behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="300"
mc:Ignorable="d">
<StackPanel
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Background="Red"
    Orientation="Vertical">
    <ToggleButton
        x:Name="CustomButton"
        Width="40"
        Height="40"
        Content="Checkbutton" />
    <Popup
        IsOpen="{Binding ElementName=CustomButton, Path=IsChecked}"
        Placement="Bottom"
        PlacementTarget="{Binding ElementName=CustomButton}"
        StaysOpen="False">
        <behaviours:Interaction.Triggers>
            <behaviours:EventTrigger EventName="Closed">
                <behaviours:ChangePropertyAction
                    PropertyName="Text"
                    TargetObject="{Binding ElementName=UrlTextBox}"
                    Value="" />
            </behaviours:EventTrigger>
        </behaviours:Interaction.Triggers>
        <TextBlock
            x:Name="UrlTextBox"
            Width="100"
            Height="100"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Background="Green"
            Foreground="White"
            Text="A url" />
    </Popup>
</StackPanel>

Теперь, если я изменю дочерний элемент всплывающего окна на пользовательский элемент управления, как показано ниже:

    <Popup
        IsOpen="{Binding ElementName=CustomButton, Path=IsChecked}"
        Placement="Bottom"
        PlacementTarget="{Binding ElementName=CustomButton}"
        StaysOpen="False">
        <behaviours:Interaction.Triggers>
            <behaviours:EventTrigger EventName="Closed">
                <behaviours:ChangePropertyAction
                    PropertyName="Text"
                    TargetObject="{Binding ElementName=CustomControl, Path=UrlTextBox}"
                    Value="" />
            </behaviours:EventTrigger>
        </behaviours:Interaction.Triggers>
        <local:CustomControl x:Name="CustomControl" />
    </Popup>

CustomControl:

<UserControl
x:Class="WpfTests.CustomControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<TextBlock
    x:Name="UrlTextBox"
    Width="100"
    Height="100"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Background="Green"
    Foreground="White"
    Text="A url" />

Поведение завершается сбоем, поскольку не удается найти текст свойства, и я не знаю почему. Я знаю, что могу сделать это с помощью кода, но я хотел бы сделать это в xaml.

Любая помощь будет оценена

1 Ответ

1 голос
/ 04 мая 2020

Эта строка не будет работать

TargetObject="{Binding ElementName=CustomControl, Path=UrlTextBox}"

Поскольку часть Path должна указывать на свойство (TextBlock внутри UserControl компилируется как поле publi c).

Вам необходимо добавить в свой код UserControl строку, подобную этой:

public TextBlock UrlText => this.UrlTextBox;

, а затем в своем поведении изменить ее на эту

TargetObject="{Binding ElementName=CustomControl, Path=UrlText}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...