【发布时间】:2022-01-20 05:05:21
【问题描述】:
我想从 UserControl 绑定到 DataTemplate。
但我得到错误
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')
我的代码
<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>
我想将 FocusedElement 绑定到 ElementName=LoginTextBox。我该怎么做?有可能吗?
【问题讨论】:
-
“有可能吗?” - 不,不是
-
@ASh :( 我们将不得不放弃数据模板并使用触发器