拥有带模板选择器的ListView
当选择此ListBoxItem(点击或箭头)时,我需要的是名为tbStringSVfieldValue的TextBox获得焦点?
<DataTemplate x:Key="fieldTemplateStringSV">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Text="Enter key to save. Tab to get the first match from existing values. Value is only committed on Enter key." VerticalAlignment="Center" Style="{StaticResource Italic}"/>
<ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" VerticalContentAlignment="Stretch"
HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<TextBox x:Name="tbStringSVfieldValue" BorderBrush="SteelBlue" BorderThickness="2" TextWrapping="Wrap"
Text="{Binding Path=FieldValue, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource stringTabConverter}}"
Validation.Error="Validataion_Error" AcceptsReturn="True" AcceptsTab="True"
PreviewKeyDown="tbStringSVfieldValue_PreviewKeyDown"/>
<!--, Converter={StaticResource stringTabConverter}-->
</ScrollViewer>
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4" Text="Existing values. Click to populate the value above." VerticalAlignment="Center" Style="{StaticResource Italic}"/>
<ListBox Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" Height="150" Margin="5,2,0,0" x:Name="lbSVtextPast" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.IsDeferredScrollingEnabled="True"
FontStyle="Normal"
ItemsSource="{Binding Path=PastEntries}" SelectedItem="{Binding Path=SelectedPastEntry}"
SelectionChanged="lbSVtextPastSelectionChanged"/>
</Grid>
</DataTemplate>
上面我试过
<Grid FocusManager.FocusedElement="{Binding ElementName=svTBsv}">
<ScrollViewer x:Name="svTBsv" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" VerticalContentAlignment="Stretch"
HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"
FocusManager.FocusedElement="{Binding ElementName=tbStringSVfieldValue}">
<TextBox x:Name="tbStringSVfieldValue" BorderBrush="SteelBlue"
下面注意不起作用
this.tbStringSVfieldValueStringSV为空
它只存在于模板中
需要以某种方式访问正确的模板
请注意ListBox中将有多个DataTemplate x:Key =“fieldTemplateStringSV”
private void lbCurDocFields_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("lbCurDocFields_SelectionChanged " + sender.GetType().ToString());
if (sender is ListBox)
{
ListBox lb = (ListBox)sender;
//lb.Template.VisualTree;
MessageBox.Show(lb.Template.ToString());
if (lb.SelectedItem != null)
{
MessageBox.Show(lb.SelectedItem.GetType().ToString());
if (lb.SelectedItem is GabeLib.DocFieldStringSV)
{
if (this.tbStringSVfieldValueStringSV != null)
this.tbStringSVfieldValueStringSV.Focus();
}