UserControl Binding必须在每次使用时设置上下文

时间:2018-01-29 17:49:44

标签: xaml binding user-controls datacontext xbind

每次我创建一个UserControl我必须在每次使用时专门设置DataContext。即使我知道父母的背景是正确的。 问题是我还需要在DataTemplates中使用它们,它们永远不会工作。 我已经尝试将堆栈面板的上下文设置为" elementname = root"但是没有解决它......

视图中定义的控件

    function testPromise() {  
    let p1 = new Promise(
        // The resolver function is called with the ability to resolve or
        // reject the promise
       (resolve, reject) => {
            /*your async function*/  
      }
    );
  // defined what to do when the promise is resolved with the then() call,
    // and what to do when the promise is rejected with the catch() call
    p1.then(
        // Log the fulfillment value
        function(val) {
            log.insertAdjacentHTML('beforeend', val +
                ') Promise fulfilled (<small>Async code terminated</small>)<br/>');
        })
    .catch(
        // Log the rejection reason
       (reason) => {
            console.log('Handle rejected promise ('+reason+') here.');
        });
}

用户控制

<cntls:ItemSpecLookup ItemGUID="{Binding NewName,Mode=TwoWay}"
DataContext="{x:Bind ViewModel,Mode=TwoWay}"
AllowFocusOnInteraction="True"
Grid.Row="0"/>

代码背后

<UserControl
    x:Class="DesignTools.Controls.ItemSpecLookup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DesignTools.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:MIISiteDataModel.DataModels"
    xmlns:converters="using:DesignTools.Converters"
    mc:Ignorable="d"
    x:Name="root">

    <UserControl.Resources>
        <converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <converters:BooleanToInvertVisibilityConverter x:Key="BooleanToInvertVisibilityConverter"/>
        <converters:MoneyConverter x:Key="MoneyConverter"/>
    </UserControl.Resources>


    <StackPanel>


        <AutoSuggestBox x:Name="ASB"
                        QueryIcon="Find"

                        Margin="0,24,48,0"
                        PlaceholderText="Search for Item" 
                        TextMemberPath="ItemPartNumber"
                        QuerySubmitted="ItemSearchQuerySubmitted"
                        TextChanged="ItemSearchSearching"
                        SuggestionChosen="ItemSearchChosen">
        <AutoSuggestBox.ItemTemplate>

            <DataTemplate x:DataType="data:ItemSpecificationData">
                <UserControl>


                    <RelativePanel x:Name="rp" SizeChanged="rp_SizeChanged">

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup>
                                <VisualState x:Name="DefaultState">
                                    <VisualState.StateTriggers>
                                        <AdaptiveTrigger MinWindowWidth="0"/>
                                    </VisualState.StateTriggers>
                                    <VisualState.Setters>
                                        <Setter Target="Cost.Opacity"  Value="0"/>
                                        <Setter Target="Description.Opacity" Value="0"/>
                                        <Setter Target="Model.Opacity" Value="0"/>
                                    </VisualState.Setters>
                                </VisualState>
                                <VisualState x:Name="Compact">
                                    <VisualState.StateTriggers>
                                        <AdaptiveTrigger MinWindowWidth="900"/>
                                    </VisualState.StateTriggers>
                                    <VisualState.Setters>
                                        <Setter Target="Cost.Opacity" Value="1"/>
                                        <Setter Target="Description.Opacity" Value="1"/>
                                        <Setter Target="Model.Opacity" Value="1"/>
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <TextBlock x:Name="Make" 
                                   Text="{x:Bind ItemManufacturer,Mode=OneWay}"
                                   Style="{StaticResource SubtitleTextBlockStyle}"
                                   Foreground="{ThemeResource SystemControlForegroundAccentBrush}"/>

                        <TextBlock x:Name="Cost"
                                   Text="{x:Bind ItemCost,Mode=OneWay,Converter={StaticResource MoneyConverter}}"
                                   Style="{StaticResource BodyTextBlockStyle}"
                                   Foreground="{StaticResource MIIGreen}"
                                   Margin="0,2,0,0"
                                   Visibility="{x:Bind ItemDiscontinued,Mode=OneWay,Converter={StaticResource BooleanToVisibilityConverter}}"
                                   HorizontalTextAlignment="Left"
                                   RelativePanel.AlignRightWithPanel="True"
                                   RelativePanel.AlignTopWithPanel="True"/>

                        <TextBlock x:Name="Discontinued"
                                   Text="Discontinued"
                                   Style="{StaticResource BodyTextBlockStyle}"
                                   Foreground="{StaticResource MIIRed}"
                                   Margin="0,2,0,0"
                                   Visibility="{x:Bind ItemDiscontinued,Mode=OneWay,Converter={StaticResource BooleanToInvertVisibilityConverter}}"
                                   RelativePanel.AlignRightWithPanel="True"
                                   RelativePanel.AlignTopWithPanel="True"/>

                        <Image x:Name="IQIcon" 
                               Source="/Assets/IQCategories/Icon_Computers256.png" 
                               Stretch="UniformToFill" 
                               Margin="10,10,10,10"
                               Height="24" 
                               Width="24"
                               RelativePanel.Below="Make"/>

                        <TextBlock x:Name="Part" 
                                   Style="{StaticResource BodyTextBlockStyle}"
                                   Margin="4,2,0,0"
                                   RelativePanel.AlignTopWith="IQIcon"
                                   RelativePanel.RightOf="IQIcon">
                            <Run Text="Part: "/>
                            <Run Text="{x:Bind ItemPartNumber,Mode=OneWay}"/>
                        </TextBlock>

                        <TextBlock x:Name="Model" Style="{StaticResource BodyTextBlockStyle}"
                                   Foreground="{StaticResource MIIGray}"
                                   Margin="12,2,0,0"
                                   RelativePanel.AlignTopWith="Part"
                                   RelativePanel.RightOf="Part">
                            <Run Text="Model: "/>
                            <Run Text="{x:Bind ItemModel,Mode=OneWay}"/>
                        </TextBlock>

                        <TextBlock x:Name="Description"
                                   Text="{x:Bind ItemDescription,Mode=OneWay}"
                                   Style="{StaticResource CaptionTextBlockStyle}"
                                   Foreground="{StaticResource MIIGray}"
                                   TextWrapping="NoWrap"
                                   Margin="4,4,0,4"
                                   TextTrimming="CharacterEllipsis"
                                   RelativePanel.Below="Part"
                                   RelativePanel.RightOf="IQIcon"/>

                    </RelativePanel>

                </UserControl>
            </DataTemplate>



        </AutoSuggestBox.ItemTemplate>

    </AutoSuggestBox>

    </StackPanel>
</UserControl>

1 个答案:

答案 0 :(得分:1)

  

不要使用:&#34; this.DataContext = this&#34;

我知道已经有一百万个关于这个的帖子了但是我把它放在这里所以人们做不了我做的哈哈