LostFocus绑定不适用于模板

时间:2010-03-11 13:15:23

标签: .net binding templates lostfocus

我有一个TabControl,它是绑定到列表的数据。 (列表中的两个数据项) TabControl有一个DataTemplate,它包含两个绑定到我的数据类上的两个字符串属性的TextBox控件。

如果我在第一个选项卡上的Textbox1上键入一些文本,然后单击Tab2,则不会更新数据源并且更改将丢失。

这是因为TextBox1实际上没有触发LostFocus(它在TextBox2上,因为移动到Tab 2自动聚焦TextBox1),我相信这是因为每个标签共享模板中的相同TextBox,只是更改了Tab上的DataContext开关。

这里有两个事件可以帮助:PreviewLostKeyboardFocus和DataContextChanged 两者仍然可以在TextBox上输入文字。

工具栏“保存”按钮也存在一个古老的问题,它无法获得焦点。

在我看来,Binding类可能应该监听PreviousLostKeyboardFocus而不是LostFocus,这似乎在上述两种场景中都会出现。

你们如何解决这些问题?

Aos,有哪些方法可以利用PreviewLostKeyboardFocus事件来更新源? (我正在考虑一个顶级容器的内容,观察这个事件,检查OriginalSource上是否有一个LostFocus UpdateSourceTrigger绑定并欺骗/强制绑定更新源 - 但我是Wpf的新手可能还有其他考虑因素我没想过)

以下是一个示例应用...

<Window x:Class="BindingFocusTabIssue.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="398" Width="731" >

<Grid>
    <TabControl Height="267" HorizontalAlignment="Left" Margin="31,36,0,0" Name="tabControl1" VerticalAlignment="Top" Width="472" ItemsSource="{Binding}">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Margin="3" Name="TextBox1" Text="{Binding Text1}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" DataContextChanged="TextBox_DataContextChanged"/>
                    <TextBox Margin="3" Name="TextBox2" Text="{Binding Text2}" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus" PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus" DataContextChanged="TextBox_DataContextChanged"/>
                </StackPanel>
            </DataTemplate>
        </TabControl.ContentTemplate>

    </TabControl>
    <GroupBox Header="List item 1" Height="115" HorizontalAlignment="Left" Margin="509,50,0,0" Name="groupBox1" VerticalAlignment="Top" Width="188">
        <Grid >
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,21,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Path=[0].Text1}" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,50,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Path=[0].Text2}"/>
        </Grid>
    </GroupBox>
    <GroupBox Header="List item 2" Height="115" HorizontalAlignment="Left" Margin="509,184,0,0" Name="groupBox2" VerticalAlignment="Top" Width="188">
        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,21,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" Text="{Binding Path=[1].Text1}" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="28,50,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" Text="{Binding Path=[1].Text2}"/>
        </Grid>
    </GroupBox>
    <ToolBar Height="26" HorizontalAlignment="Left" Margin="54,3,0,0" Name="toolBar1" VerticalAlignment="Top" Width="200">
        <Button>Save</Button>
    </ToolBar>
</Grid>

及其代码隐藏

using System;

使用System.Collections.Generic; 使用System.Diagnostics; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Input;

命名空间BindingFocusTabIssue {     public partial class MainWindow:Window     {         public MainWindow()         {             的InitializeComponent();

        DataContext = new List<DataClass>
                        {
                            new DataClass { Name="One"},
                            new DataClass { Name="Two"},
                        };
    }

    private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        var textBox = (TextBox) e.Source;
        Debug.WriteLine(textBox.Name + "_GotFocus and Text='" + textBox.Text + "'");
    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        var textBox = (TextBox) e.Source;
        Debug.WriteLine(textBox.Name + "_LostFocus and Text='" + textBox.Text + "'");
    }

    private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        var textBox = (TextBox) e.Source;
        Debug.WriteLine(textBox.Name + "_PreviewLostKeyboardFocus and Text='" + textBox.Text + "'");
    }

    private void TextBox_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var textBox = (TextBox) sender;
        Debug.WriteLine(textBox.Name + "_DataContextChanged and Text='" + textBox.Text + "'");
    }
}

public class DataClass
{
    public string Name { get; set; }
    public string Text1 { get; set; }
    public string Text2 { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

}

0 个答案:

没有答案