我的装订无法正常工作 [英] My binding doesn't work correctly

查看:75
本文介绍了我的装订无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个使用大量数据绑定的C#WPF,除了一个绑定之外,它们都可以工作.它将文本框绑定到字符串.但是,当我键入某些内容时,除我关闭表单外,该内容不会反映到属性中.

Hi I have a C# WPF which uses lots of data bindings and they all work except for one. It binds a textbox to a string. but when i type something it is not reflected to the property except for when i close the form.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace InfoManager.Model
{
    public class TagEditor : INotifyPropertyChanged
    {
        public ObservableCollection<TagTick> tags = new ObservableCollection<TagTick>();
        public string _filterText="";
        public string FilterText
        {
            get { return _filterText; }
            set { _filterText = value; }  //where i set the breakpoint
        }
        private InfoItem infoItem;
        private TotalTagFamilyCollection _totalTagFamilyCollection;
        public InfoManager.Model.TotalTagFamilyCollection TotalTagFamilyCollection
        {
            get { return _totalTagFamilyCollection; }
            set { _totalTagFamilyCollection = value; }
        }
......




数据上下文代码:




codes for data context:

namespace InfoManager.Presentation_Layer
{
    /// &lt;summary&gt;
    /// Interaction logic for WindowTagAttaching.xaml
    /// &lt;/summary&gt;
    public partial class WindowTagAttaching : Window
    {
        TagEditor _tagEditor;
        TagEditor TagEditor
        {
            set { _tagEditor = value; }
            get { return _tagEditor; }
        }
        public WindowTagAttaching(InfoItem infoItem, TotalTagFamilyCollection totalTagFamilyCollection)
        {
            InitializeComponent();
            _tagEditor = new TagEditor(infoItem, totalTagFamilyCollection);
            this.DataContext = TagEditor;

        }
..........









<Window x:Class="InfoManager.Presentation_Layer.WindowTagAttaching"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WindowTagAttaching" Height="300" Width="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="28,31,0,0" Name="textBlock1" Text="Filter Tags:" VerticalAlignment="Top" />
        <TextBox Text="{Binding Path=FilterText,Mode=TwoWay}"  Height="23" HorizontalAlignment="Left" Margin="91,28,0,0" Name="textBoxFilterTags" VerticalAlignment="Top" Width="120" TextChanged="textBoxFilterTags_TextChanged" />
        <ListBox ItemsSource="{Binding Path=Filter,UpdateSourceTrigger=Explicit}"  Height="178" HorizontalAlignment="Left" Margin="12,71,0,0" Name="listBoxTags" VerticalAlignment="Top" Width="254">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Path=Tick,Mode=TwoWay}" Margin="3,1,1,1" />
                        <TextBlock Text="{Binding Path=Name}" Margin="3,1,1,1" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

推荐答案

那是因为默认情况下,仅当控件失去焦点时才更新源,因此需要将绑定的UpdateSourceTrigger设置为PropertyChanged

That''s because by default your source is only updated when your control loses focus, you need to set the binding''s UpdateSourceTrigger to PropertyChanged

<textbox text="{Binding Path=FilterText,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" height="23" horizontalalignment="Left" margin="91,28,0,0" name="textBoxFilterTags" verticalalignment="Top" width="120" textchanged="textBoxFilterTags_TextChanged" />



我在这里的文章将来可能会帮助您解决绑定问题
调试WPF数据绑定 [



My article here might help you with binding issues in the future
Debugging WPF data bindings[^]
No, thats not advetisement :D


这篇关于我的装订无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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