简体   繁体   English

我可以从 UserControl 绑定到 DataTemplate 吗?

[英]Can I Bind from UserControl to DataTemplate?

I want to bind from UserControl to DataTemplate.我想从 UserControl 绑定到 DataTemplate。

But I get error但我得到错误

Cannot find source for binding with reference 'ElementName=LoginTextBox'. BindingExpression:(no path); DataItem=null; target element is 'Grid' (Name=''); target property is 'FocusedElement' (type 'IInputElement')

My code我的代码

<UserControl>
    <UserControl.Resources>
        
        <DataTemplate DataType="{x:Type models:LocalAuthenticationMethod}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="72"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" VerticalAlignment="Center"
                       Text="{uiCommon:LocalizationBinding ResourceName=Login}"
                       FontSize="{DynamicResource LoginWindowPropertiesFontSize}"/>
                
                <!-- bind to here -->
                <ComboBox x:Name="LoginTextBox"
                      Grid.Column="1"
                      Height="24"
                      Text="{Binding ElementName=Root, Path=DataContext.Login, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"
                      ItemsSource="{Binding ElementName=Root, Path=DataContext.AvailableUsers, Mode=OneWay}"
                      IsEditable="True"
                      behavior:FocusAdvancement.AdvancesByEnterKey="True"
                      FontSize="{DynamicResource LoginWindowPropertiesFontSize}">
                    <b:Interaction.Behaviors>
                        <behavior:SelectTextOnGotFocusBehavior/>
                        <behavior:SelectTextOnTargetUpdatedBehavior/>
                    </b:Interaction.Behaviors>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="Root">

        <!-- how to bind here? -->
        <Grid FocusManager.FocusedElement="{Binding ElementName=LoginTextBox}"> 
            <ContentControl>
                <ContentPresenter Content="{Binding SelectedAuthenticationMethod, Mode=OneWay}"/>
            </ContentControl>
        </Grid>
    </Grid>
</UserControl>

I want to bind FocusedElement to ElementName=LoginTextBox.我想将 FocusedElement 绑定到 ElementName=LoginTextBox。 How can I do it?我该怎么做? Is it possible at all?有可能吗?

Is it possible at all?有可能吗?

No, because the name "LoginTextBox" is only available inside the DataTemplate where the ComboBox is defined.不,因为名称“LoginTextBox”仅在定义了ComboBoxDataTemplate中可用。

The Grid resides in a different XAML namescope that doesn't know anything about the individual elements that defines the visual appearance of a LocalAuthenticationMethod object. Grid位于不同的XAML 名称范围中,该名称范围对定义LocalAuthenticationMethod object 的视觉外观的各个元素一无所知。

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

相关问题 将DataTemplate的内容绑定到另一个userControl - Bind the content of DataTemplate to another userControl 将 DataTemplate 中的 WPF RelayCommand 绑定到 UserControl 中的按钮 - Bind a WPF RelayCommand from DataTemplate to a Button inside a UserControl 从DataTemplate获取UserControl - Get the UserControl from DataTemplate FlipView 数据模板无法绑定到自定义 UserControl - FlipView Datatemplate fails to bind on custom UserControl UWP将ListView的项目绑定为UserControl DataTemplate - UWP bind a ListView's item as UserControl DataTemplate 我可以将类型类型的依赖项属性绑定/设置为DataTemplate的DataType吗 - Can I bind/set a dependency property of type Type to DataType of a DataTemplate 我怎样才能{x:绑定}到UWP中的DataTemplate的根类型? - How can I {x:Bind} to a DataTemplate's root type in UWP? 如何将数据模板绑定到可观察的集合,以便它们全部显示? - How can I bind a datatemplate to a observablecollection so that they all show up? 如何将DataTemplate中的ComboBox ItemSource绑定到列表? - How can I bind ComboBox ItemSource in a DataTemplate to a List? 可以从ItemsControl的数据模板绑定组合框Itemssource - Can one bind a combobox Itemssource from a datatemplate of a ItemsControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM