简体   繁体   English

Xaml标记绑定在Windows 8.1 Store App的按钮弹出按钮中失败

[英]Xaml Tag binding fails in button flyout for windows 8.1 store app

I have a windows 8.1 store app and I can't figure out how to get the datacontext to access my viewmodel for a relaycommand. 我有一个Windows 8.1应用商店应用程序,我不知道如何获取数据上下文来访问我的视图模型以进行中继命令。 The xaml works all the way up to the flyout and then it fails. xaml一直运行到弹出为止,然后失败。 So the button with content "buttonWorks" successfully gets the binding from the rootGrid element and calls the command on the viewmodel. 因此,内容为“ buttonWorks”的按钮成功地从rootGrid元素获取了绑定,并在视图模型上调用了命令。 But the button right below in the flyout does not. 但是弹出按钮下方的按钮没有。 Why? 为什么?

To make an example of the binding problem, create a new Store App with the Split App template. 为了举例说明绑定问题,请使用“拆分应用程序”模板创建一个新的“商店应用程序”。 Then add a flyout to the SplitPage and bind a textbox text to the itemtitle element. 然后将弹出按钮添加到SplitPage,并将文本框文本绑定到itemtitle元素。 The binding will not populate the flyout textbox. 绑定将不会填充弹出文本框。 The code snippet looks like this: 该代码段如下所示:

<Image Source="{Binding ImagePath}" Grid.Row="1" Margin="0,0,20,0" Width="180" Height="180" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
    <StackPanel x:Name="itemDetailTitlePanel" Grid.Row="1" Grid.Column="1">
        <TextBlock x:Name="itemTitle" Margin="0,-10,0,0" Text="{Binding Title}" Style="{StaticResource SubheaderTextBlockStyle}"/>
        <TextBlock x:Name="itemSubtitle" Margin="0,0,0,20" Text="{Binding Subtitle}" Style="{StaticResource SubtitleTextBlockStyle}"/>
            <Button Content="Update">
                <Button.Flyout>
                    <Flyout>
                        <StackPanel>
                            <StackPanel Margin="5" Orientation="Vertical"  >
                                <TextBlock  Text="Item Title" VerticalAlignment="Bottom"/>
                                <TextBox Text="{Binding ElementName=itemTitle,Path=Text,Mode=TwoWay}"  Width="200" />
                            </StackPanel>
                       </StackPanel>
                   </Flyout>
              </Button.Flyout>
          </Button>
       </StackPanel>

I can get the command binding to work using Dani's answer and setting the datacontext on the button hosting the flyout like this, but I can't bind the textboxes as seen in the template example above 我可以使用Dani的答案并在托管弹出按钮的按钮上设置datacontext来使命令绑定正常工作,但是我无法绑定文本框,如上面的模板示例所示

<Button Content="Update" HorizontalAlignment="Center" DataContext="{Binding ViewModel, ElementName=pageRoot}" >
<Button.Flyout>
    <Flyout>
        <StackPanel>
            <TextBlock  Text="Item Title" VerticalAlignment="Bottom"/>
            <TextBox Text="{Binding ElementName=itemTitle,Path=Text,Mode=TwoWay}"  Width="200" />

            <Button x:Name="buttonTest" Margin="5" Height="40" Content="Test" HorizontalAlignment="Center"   Command="{Binding UpdateMatchDataCommand}"/>

Original Code Example Problem: 原始代码示例问题:

<Grid Name="rootGrid" Tag="{Binding}"  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

....

   <ScrollViewer
        x:Name="itemDetail"
        AutomationProperties.AutomationId="ItemDetailScrollViewer"
        Grid.Row="0"
        Grid.Column="1"
        Grid.RowSpan="2"
        Padding="60,0,66,0"
        HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"
        DataContext="{Binding SelectedItem, ElementName=itemListView}"

       <Grid x:Name="itemDetailGrid" Margin="0,60,0,50">
           <StackPanel Orientation="Vertical" Grid.Row="1"  Margin="0,0,10,0" Width="180" Height="180">
                <Button Name="buttonWorks" Content="Test" Command="{Binding Tag.UpdateMatchDataCommand,ElementName=rootGrid}"/>

              <Button Content="Update" HorizontalAlignment="Center" >
                  <Button.Flyout>
                      <Flyout>
                          <Button Name="buttonFail" Content="Test" Command="{Binding Tag.UpdateMatchDataCommand,ElementName=rootGrid}"/>
                      </Flyout>
                  </Button.Flyout>
              </Button>
          </StackPanel>


....

If you work with ViewModels I would suggest that you change the DataContext to your page. 如果使用ViewModels,建议您将DataContext更改为页面。

<Page
    x:Name="rootPage"
    DataContext="{Binding ViewModel, RelativeSource={RelativeSource Self}}"
...

And CodeBehind: 和代码背后:

private readonly ViewModel _viewModel = new ViewModel();
public ViewModel ViewModel
{
    get { return _viewModel; }
}

Now you can change your commands to: 现在,您可以将命令更改为:

<Button Name="buttonWorks" Content="Test" Command="{Binding Tag.UpdateMatchDataCommand}"/>

ElementName is now only necessary if you bind to a property in code behind. 现在,仅当您绑定到后面代码中的属性时,才需要ElementName。

It ends up that I could never solve the problem using the binding that the template provides. 最后,我永远无法使用模板提供的绑定解决问题。 It appears to me that changing the datacontext of a single element in the flyout is not allowed. 在我看来,不允许在弹出窗口中更改单个元素的数据上下文。 So, the way I got around it is to create a selected item in the viewmodel and changed the template code to bind to the selected item on view model. 因此,我解决的方法是在视图模型中创建选定的项目,然后更改模板代码以绑定到视图模型中的选定项目。 Then bind all elements including the command to the same datacontext for the entire form. 然后将包括命令在内的所有元素绑定到整个表单的相同数据上下文。

    <ListView
        x:Name="itemListView"
        AutomationProperties.AutomationId="ItemsListView"
        AutomationProperties.Name="Items"
        TabIndex="1"
        Grid.Row="1"
        Margin="-10,-10,0,0"
        Padding="120,0,0,60"
        ItemsSource="{Binding Matches}"
        SelectedItem="{Binding SelectedMatch, Mode=TwoWay}"
        IsSwipeEnabled="False">
        <ListView.ItemTemplate>
            <DataTemplate>

Now the binding works for all fields including the template. 现在,绑定适用于包括模板在内的所有字段。

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

相关问题 如何在通用Windows Store应用中为按钮弹出进行相对绑定 - How to do relative binding for button flyout in universal windows store app 如何在Windows 8.1 Store应用程序的设置弹出框中进行导航? - How to navigate within settings flyout in Windows 8.1 Store app? 列出windows store 8.1 app中菜单弹出窗口中的所有字体和颜色 - List all fonts and colors in menu flyout in windows store 8.1 app 将弹出窗口设置为主框架导航(Windows 8.1应用程序商店) - Setting Flyout to Main Frame Navigation(Windows 8.1 app store) Windows 8.1 XAML存储应用程序的运行时更改资源字典 - Runtime change Resource Dictionary for a Windows 8.1 XAML Store App 如何将Windows商店中的Windows手机应用程序(8.1 XAML)迁移到8.1 Silverlight? - How to Migrate a windows phone app(8.1 XAML) which is live in windows store to 8.1 Silverlight? Windows 8.1将xaml以字符串形式保存InkManager - Windows 8.1 store xaml save InkManager in a string 如何使用C#和xaml在Windows Store 8.1应用程序中自定义Windows AAD登录屏幕? - How to customize the windows AAD Login screen in windows store 8.1 app using C# and xaml? Windows 10应用程序存在于Windows 10应用商店中,但Windows 8.1应用商店中不存在 - Windows 8.1 app exists in Windows 10 Store, but not in Windows 8.1 Store Windows Store 8.1动态绑定未在UI中更新 - Windows store 8.1 dynamic binding not updating in UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM