简体   繁体   English

为什么我的XAML交互行为无法在用户控件中起作用

[英]Why won't my XAML interactivity behaviors work in a user control

I'm working on a UWP application that utilizes interactivity behaviors to open/close UI elements. 我正在使用一种UWP应用程序,该应用程序利用交互行为来打开/关闭UI元素。

As a little back story my application previously used copy/pasted XAML for a customer selection bladeview and blades in all of the relevant views. 作为一个小故事,我的应用程序以前使用复制/粘贴XAML来获得客户选择的刀刃视图和所有相关视图中的刀刃。

Within that XAML were interactivity behaviors/triggers that would close one blade and open another upon a button click that worked perfectly. 在XAML中,是交互行为/触发器,当单击按钮时,交互行为/触发器将关闭一个刀片,然后打开另一个刀片。

Given the mess that was copy pasting the same XAML all over the places I cleaned up that UI piece and moved it into it's own user control which I just managed to get to work the way I need it to with bindings and all. 鉴于在整个地方复制粘贴相同XAML的混乱局面,我清理了该UI并将其移到它自己的用户控件中,我设法通过绑定和所有方法按我需要的方式进行工作。 The only issue is that my interactivity behaviors/triggers no longer work now that they are part of a user control. 唯一的问题是我的交互行为/触发器已成为用户控件的一部分,因此不再起作用。

Here is the specific XAML that relates to the behviors/triggers within the button: 这是与按钮内的行为/触发器有关的特定XAML:

<Button x:Name="CreatNewCustomer"
                            HorizontalAlignment="Stretch"
                            Content="New Customer"
                            VerticalAlignment="Stretch"
                            Height="45"
                            Background="#FF007ACC"
                            Width="auto"
                            Foreground="#FFCDCDCD"
                            Margin="5">
                        <interactivity:Interaction.Behaviors>
                            <core:EventTriggerBehavior EventName="Click">
                                <core:ChangePropertyAction  TargetObject="NewCustomerBlade"
                                                            PropertyName="IsOpen"
                                                            Value="True" />
                            </core:EventTriggerBehavior>
                        </interactivity:Interaction.Behaviors>
                    </Button>

Here is my entire user control: 这是我的整个用户控件:

<UserControl x:Class="myapp.Views.CustomerDatabaseControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="using:retailemployeetoolset.Views"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
         xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
         xmlns:core="using:Microsoft.Xaml.Interactions.Core"
         xmlns:models="using:myapp.Models"
         mc:Ignorable="d"
         d:DesignHeight="840"
         d:DesignWidth="1320">

<Grid x:Name="AddCustomerBlades">

    <controls:BladeView x:Name="CustomerSearchBladeview"
                        IsEnabled="True"
                        BladeMode="Fullscreen">
        <controls:BladeItem x:Name="SearchCustomerBlade"
                            Width="1325"
                            Background="#FF1E1E1E"
                            Height="712"
                            TitleBarVisibility="Collapsed"
                            IsOpen="True"
                            BorderThickness="5"
                            BorderBrush="#FF707070">
            <StackPanel>
                <StackPanel Orientation="Horizontal"
                            Height="65">
                    <TextBlock Text="Customers"
                               FontSize="38"
                               Margin="10"
                               Foreground="#FFCDCDCD"
                               Width="825" />
                    <StackPanel Orientation="Horizontal"
                                Width="465">
                        <TextBlock Text="Search: "
                                   FontSize="28"
                                   Margin="5"
                                   Foreground="#FFCDCDCD"
                                   Width="90"
                                   VerticalAlignment="Center" />
                        <TextBox x:Name="CustomerSearch"
                                 TextWrapping="Wrap"
                                 Height="46"
                                 Width="350"
                                 Margin="5" />
                    </StackPanel>
                </StackPanel>
                <StackPanel Orientation="Horizontal"
                            Margin="5"
                            HorizontalAlignment="Right">
                    <Button x:Name="CreatNewCustomer"
                            HorizontalAlignment="Stretch"
                            Content="New Customer"
                            VerticalAlignment="Stretch"
                            Height="45"
                            Background="#FF007ACC"
                            Width="auto"
                            Foreground="#FFCDCDCD"
                            Margin="5">
                        <interactivity:Interaction.Behaviors>
                            <core:EventTriggerBehavior EventName="Click">
                                <core:ChangePropertyAction  TargetObject="NewCustomerBlade"
                                                            PropertyName="IsOpen"
                                                            Value="True" />
                            </core:EventTriggerBehavior>
                        </interactivity:Interaction.Behaviors>
                    </Button>
                    <Button x:Name="CloseCustomerWindow"
                            HorizontalAlignment="Stretch"
                            Content="Close Window"
                            VerticalAlignment="Stretch"
                            Height="45"
                            Background="#FF007ACC"
                            Width="auto"
                            Foreground="#FFCDCDCD"
                            Margin="5" />
                </StackPanel>
                <StackPanel Margin="0,5"
                            Orientation="Horizontal">
                    <StackPanel Width="1307">
                        <GridView Name="CustomerList"
                                  ItemsSource="{Binding CustomerDatabase}"
                                  SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"
                                  Width="auto"
                                  Height="685"
                                  Background="#FF2E2B2B"
                                  BorderThickness="3"
                                  BorderBrush="#FF707070"
                                  Margin="10,0,8,0">
                            <GridView.Header>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="254" />
                                        <ColumnDefinition Width="254" />
                                        <ColumnDefinition Width="254" />
                                        <ColumnDefinition Width="254" />
                                        <ColumnDefinition Width="254" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto" />
                                        <RowDefinition Height="8" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0"
                                               Grid.Column="0"
                                               Text="First Name"
                                               TextAlignment="Center"
                                               FontWeight="Bold"
                                               FontSize="16"
                                               Margin="7,0,0,1"
                                               Foreground="#FFCDCDCD" />
                                    <TextBlock Grid.Row="0"
                                               Grid.Column="1"
                                               Text="Last Name"
                                               TextAlignment="Center"
                                               FontWeight="Bold"
                                               FontSize="16"
                                               Margin="7,0,0,1"
                                               Foreground="#FFCDCDCD" />
                                    <TextBlock Grid.Row="0"
                                               Grid.Column="2"
                                               Text="Enail"
                                               TextAlignment="Center"
                                               FontWeight="Bold"
                                               FontSize="16"
                                               Margin="7,0,0,1"
                                               Foreground="#FFCDCDCD" />
                                    <TextBlock Grid.Row="0"
                                               Grid.Column="3"
                                               Text="Phone Number"
                                               TextAlignment="Center"
                                               FontWeight="Bold"
                                               FontSize="16"
                                               Margin="7,0,0,1"
                                               Foreground="#FFCDCDCD" />
                                    <TextBlock Grid.Row="0"
                                               Grid.Column="4"
                                               Text="Organization"
                                               TextAlignment="Center"
                                               FontWeight="Bold"
                                               FontSize="16"
                                               Margin="7,0,0,1"
                                               Foreground="#FFCDCDCD" />
                                </Grid>
                            </GridView.Header>
                            <GridView.ItemContainerStyle>
                                <Style TargetType="GridViewItem">
                                    <Setter Property="Margin"
                                            Value="7,0,0,1" />
                                    <Setter Property="HorizontalAlignment"
                                            Value="Center" />
                                </Style>
                            </GridView.ItemContainerStyle>
                            <GridView.ItemTemplate>
                                <DataTemplate x:DataType="models:Customer">
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="254" />
                                            <ColumnDefinition Width="254" />
                                            <ColumnDefinition Width="254" />
                                            <ColumnDefinition Width="254" />
                                            <ColumnDefinition Width="254" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="45" />
                                        </Grid.RowDefinitions>


                                        <Border BorderThickness="2"
                                                BorderBrush="#FF575454"
                                                Grid.Column="0">
                                            <TextBlock Text="{x:Bind CustomerFirstName}"
                                                       Foreground="#FFCDCDCD"
                                                       Width="auto"
                                                       Margin="0,8,0,-2"
                                                       TextAlignment="Center" />
                                        </Border>
                                        <Border BorderThickness="2"
                                                BorderBrush="#FF575454"
                                                Grid.Column="1">
                                            <TextBlock Text="{x:Bind CustomerLastName}"
                                                       Foreground="#FFCDCDCD"
                                                       Width="auto"
                                                       Margin="0,10,0,0"
                                                       TextAlignment="Center" />
                                        </Border>
                                        <Border BorderThickness="2"
                                                BorderBrush="#FF575454"
                                                Grid.Column="2">
                                            <TextBlock Text="{x:Bind CustomerEmail}"
                                                       Foreground="#FFCDCDCD"
                                                       Width="auto"
                                                       Margin="0,8,0,0"
                                                       TextAlignment="Center" />
                                        </Border>
                                        <Border BorderThickness="2"
                                                BorderBrush="#FF575454"
                                                Grid.Column="3">
                                            <TextBlock Text="{x:Bind CustomerPhoneNumber}"
                                                       Foreground="#FFCDCDCD"
                                                       Width="auto"
                                                       TextAlignment="Center" />
                                        </Border>
                                        <Border BorderThickness="2"
                                                BorderBrush="#FF575454"
                                                Grid.Column="4">
                                            <TextBlock Text="{x:Bind OrganizationName}"
                                                       Foreground="#FFCDCDCD"
                                                       Width="auto"
                                                       TextAlignment="Center" />
                                        </Border>
                                    </Grid>
                                </DataTemplate>
                            </GridView.ItemTemplate>
                        </GridView>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
        </controls:BladeItem>
        <controls:BladeItem x:Name="CustomerAddBlade"
                            Width="1320"
                            Background="#FF1E1E1E"
                            Height="835"
                            TitleBarVisibility="Collapsed"
                            IsOpen="False"
                            BorderThickness="5"
                            BorderBrush="#FF707070">
            <StackPanel>
                <TextBlock Text="Add Customer"
                           FontSize="48"
                           Foreground="#FFCDCDCD"
                           Height="56"
                           Margin="15"
                           HorizontalAlignment="Left" />
                <StackPanel  HorizontalAlignment="Center"
                             Width="409">
                    <StackPanel Margin="10,0">
                        <TextBlock Text="Email Address:"
                                   FontSize="22"
                                   Foreground="#FFCDCDCD"
                                   Height="36"
                                   Margin="3" />
                        <TextBox x:Name="EmailBox"
                                 TextWrapping="Wrap"
                                 Height="50"
                                 Margin="3" />
                    </StackPanel>
                    <StackPanel Margin="10,0">
                        <TextBlock Text="First Name:"
                                   FontSize="22"
                                   Foreground="#FFCDCDCD"
                                   Height="36"
                                   Margin="3" />
                        <TextBox x:Name="FirstNameBox"
                                 TextWrapping="Wrap"
                                 Height="50"
                                 Margin="3" />
                    </StackPanel>
                    <StackPanel Margin="10,0">
                        <TextBlock Text="Last Name:"
                                   FontSize="22"
                                   Foreground="#FFCDCDCD"
                                   Height="36"
                                   Margin="3" />
                        <TextBox x:Name="LastNameBox"
                                 TextWrapping="Wrap"
                                 Height="50"
                                 Margin="3" />
                    </StackPanel>
                    <StackPanel Margin="10,0">
                        <TextBlock Text="Phone Number:"
                                   FontSize="22"
                                   Foreground="#FFCDCDCD"
                                   Height="36"
                                   Margin="3" />
                        <TextBox x:Name="PhoneBox"
                                 TextWrapping="Wrap"
                                 Height="50"
                                 Margin="3" />
                    </StackPanel>
                    <CheckBox x:Name="BusinessCustomerCheckbox"
                              Content="Business Customer"
                              HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch"
                              Height="21"
                              Margin="10"
                              Foreground="#FFCDCDCD" />
                    <StackPanel x:Name="OrgNamePanel"
                                Visibility="Visible"
                                Margin="10,0">
                        <TextBlock Text="Organization Name:"
                                   FontSize="22"
                                   Foreground="#FFCDCDCD"
                                   Height="36"
                                   Margin="3" />
                        <TextBox x:Name="BusinessNameBox"
                                 TextWrapping="Wrap"
                                 Height="50"
                                 Margin="3" />
                    </StackPanel>
                    <Button x:Name="AddNewCustomerButton"
                            HorizontalAlignment="Stretch"
                            Content="Add Customer"
                            VerticalAlignment="Stretch"
                            Height="60"
                            Background="#FF007ACC"
                            Foreground="#FFCDCDCD"
                            Margin="20">

                    </Button>
                </StackPanel>
            </StackPanel>
        </controls:BladeItem>
    </controls:BladeView>
</Grid>

Is there something that needs to be done differently when using behaviors within a user control? 在用户控件中使用行为时,是否需要以不同的方式完成某些工作? Or can they just not be used within a user control? 还是只能在用户控件中不使用它们?

I could just create properties in the code behind for the user control to handle opening and closing my blades but that just feels like it's going to be sloppier and require a lot more code than the 8 lines of code needed to use behaviors. 我可以在后面的代码中创建属性,以供用户控件处理刀片的打开和关闭,但是这感觉会更草率,并且比使用行为所需的8行代码要多得多的代码。

EDIT : Adding the exception I received in case someone else searches for a similar error. 编辑 :添加我收到的异常,以防其他人搜索类似的错误。

"Cannot find a property named Windows.UI.Xaml.PropertyPath on type String." “在字符串类型上找不到名为Windows.UI.Xaml.PropertyPath的属性。”

I don't think TargetObject="NewCustomerBlade" would work. 我认为TargetObject="NewCustomerBlade"不起作用。 Can you try TargetObject="{Binding ElementName=NewCustomerBlade}" ? 您可以尝试TargetObject="{Binding ElementName=NewCustomerBlade}"吗?

You also need to put this behavior outside of the user control, where the NewCustomerBlade control is. 您还需要将此行为放在NewCustomerBlade控件所在的用户控件之外。 Make sure the Button and the control are on the same page. 确保按钮和控件在同一页上。

Sounds like your event handler isn't getting resolved correctly. 听起来您的事件处理程序未正确解决。 If your are specifying the click handler in a different class than your control, you can wire it up with x:bind. 如果要在与控件不同的类中指定单击处理程序,则可以使用x:bind将其连接起来。

Check out the following code taken from MSDN 查看以下从MSDN取得的代码

 <ComboBox x:Name="ColorComboBox" ItemsSource="{x:Bind SettingsVM.Colors}" SelectionChanged="{x:Bind SettingsVM.ColorDefinitionChanged(SelectedItem)}" /> void ColorDefinitionChanged(ColorDefinition def) { ... } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM