为什么我的XAML交互行为不能在用户控件中工作

时间:2017-06-29 06:10:04

标签: c# xaml mvvm user-controls uwp

我正在使用UWP应用程序,该应用程序利用交互行为来打开/关闭UI元素。

作为一个小故事,我的应用程序以前使用复制/粘贴的XAML在所有相关视图中为客户选择bladeview和刀片。

在该XAML中,交互行为/触发器将关闭一个刀片,并在按钮单击时打开另一个刀片,完美运行。

鉴于复制粘贴在同一个XAML上的混乱,我清理了那个UI片段并将其移动到它自己的用户控件中,我设法以我需要的方式工作绑定和所有。唯一的问题是我的交互行为/触发器现在不再起作用,因为它们是用户控件的一部分。

以下是与按钮中的behviors /触发器相关的特定XAML:

<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>

这是我的整个用户控件:

UNION ALL

在用户控件中使用行为时,是否需要采取不同的措施?或者它们是否可以在用户控件中使用?

我可以在后面的代码中创建属性,以便用户控制来处理我的刀片的打开和关闭,但这感觉它会变得更加笨拙并且需要比需要的8行代码更多的代码使用行为。

修改: 添加我收到的异常以防其他人搜索类似的错误。

&#34;在String类型上找不到名为Windows.UI.Xaml.PropertyPath的属性。&#34;

2 个答案:

答案 0 :(得分:2)

我不认为TargetObject="NewCustomerBlade"会起作用。你能试试TargetObject="{Binding ElementName=NewCustomerBlade}"吗?

您还需要将此行为置于NewCustomerBlade控件所在的用户控件之外。确保按钮和控件位于同一页面上。

答案 1 :(得分:1)

听起来您的事件处理程序无法正确解析。如果您在与控件不同的类中指定单击处理程序,则可以使用x:bind。

连接它

查看以下来自MSDN

的代码
<ComboBox x:Name="ColorComboBox"
      ItemsSource="{x:Bind SettingsVM.Colors}"
      SelectionChanged="{x:Bind SettingsVM.ColorDefinitionChanged(SelectedItem)}" />

void ColorDefinitionChanged(ColorDefinition def)
{
   ...
}