在WPF中关注DataTemplate [英] Focus DataTemplate in WPF

查看:166
本文介绍了在WPF中关注DataTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题:ItemsControler中的datatemplated数据,其中没有得到初步的重点。
在下面的示例中,有4个字符串,然后通过Datatemplate填充到TextBox中。


$ b 示例:

 < Window xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:sys =clr-namespace: System; assembly = mscorlib
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml>
<网格>
< ListView>
< ListView.Resources>
< DataTemplate DataType ={x:Type sys:String}>
< TextBox Width =100Text ={Binding Mode = OneWay}/>
< / DataTemplate>
< /ListView.Resources>
< ListView.ItemsSource>
< sys:String> test< / sys:String>
< sys:String> test< / sys:String>
< sys:String> test< / sys:String>
< sys:String> test< / sys:String>
< / x:Array>
< /ListView.ItemsSource>
< / ListView>
< / Grid>
< / Window>

我已经尝试了一些

<$ p
$> $ code> FocusManager.FocusedElement ={Binding ElementName = [...]}



无意义地说:没有成功。
任何人都可以得到我想要的,而不需要遍历c#中的可视化树?
应该可以做到这一点,不是吗?

解决方案

FocusManager对此很有帮助。 / p>

 < DataTemplate x:Key =MyDataTemplateDataType =ListBoxItem> 
<网格>
< WrapPanel Orientation =Horizo​​ntalFocusManager.FocusedElement ={Binding ElementName = tbText}>
< CheckBox IsChecked ={Binding Path = Completed}Margin =5/>
< Button Style ={StaticResource ResourceKey = DeleteButtonTemplate}Margin =5Click =btnDeleteItem_Click/>
TextBox Name =tbText
Text ={Binding Path = Text}
Width =200
TextWrapping =Wrap
AcceptsReturn = True
Margin =5
Focusable =True/>
< DatePicker Text ={Binding Path = Date}Margin =5/>
< / WrapPanel>
< / Grid>
< / DataTemplate>


The behaviour I am looking for is that a selection of a Item in a ListView results in focusing the first focusable visualchild.

Problem: datatemplated data in a ItemsControler which does not get an initial focus. In the example below there are 4 Strings which are then stuffed into a TextBox via Datatemplate.

Example:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <ListView>
         <ListView.Resources>
            <DataTemplate DataType="{x:Type sys:String}" >
               <TextBox Width="100" Text="{Binding Mode=OneWay}"/>
            </DataTemplate>
         </ListView.Resources>
         <ListView.ItemsSource>
            <x:Array Type="{x:Type sys:String}">
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
               <sys:String>test</sys:String>
            </x:Array>
         </ListView.ItemsSource>
      </ListView>
   </Grid>
</Window>

I already tried some combinations of

FocusManager.FocusedElement="{Binding ElementName=[...]}"

pointless to say: without success. Anyone a clou how I could get what I want without traversing the visual tree in c#? It should be possible to do this, shouldn't it?

解决方案

The FocusManager works sweet for this.

            <DataTemplate x:Key="MyDataTemplate" DataType="ListBoxItem">
            <Grid>
                <WrapPanel Orientation="Horizontal" FocusManager.FocusedElement="{Binding ElementName=tbText}">
                    <CheckBox IsChecked="{Binding Path=Completed}" Margin="5" />
                    <Button Style="{StaticResource ResourceKey=DeleteButtonTemplate}" Margin="5" Click="btnDeleteItem_Click" />
                    <TextBox Name="tbText" 
                             Text="{Binding Path=Text}" 
                             Width="200" 
                             TextWrapping="Wrap" 
                             AcceptsReturn="True" 
                             Margin="5" 
                             Focusable="True"/>
                    <DatePicker Text="{Binding Path=Date}" Margin="5"/>
                </WrapPanel>
            </Grid>
        </DataTemplate>

这篇关于在WPF中关注DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆