[英]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”仅在定义了
ComboBox
的DataTemplate
中可用。
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.