WPF双向绑定不起作用 [英] WPF two-way binding not working

查看:167
本文介绍了WPF双向绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的主窗口分配了一个数据上下文(UserPreferences),并且有一个文本框将双向绑定到上下文中数据上下文的属性之一(CollectionDevice)中的属性.

I have a data context (UserPreferences) assigned to my main window, and a textbox that binds two-way to a property within one of the data context's properties (CollectionDevice) within the context.

在加载窗口时,文本框不会绑定到模型中的属性.我在调试器中验证是否已将数据上下文设置为模型对象,并且已正确分配了模型的属性.但是,我得到的只是一系列带有0的文本框.

When the Window loads, the textbox's do not bind to the properties in my model. I verify within the debugger that the data context is set to the model object and the model's properties are properly assigned. All I get however are a series of textbox's with 0's in them.

当我在文本框中输入数据时,数据将在模型中更新.当我加载数据并将其应用于数据上下文时,该问题就发生了,文本框没有更新.

When I enter the data into the textbox's, the data is updated in the model. The issue just happens when I load the data and apply it to the data context, the text box does not get updated.

将模型保存到数据库时,将从文本框中保存正确的数据.当我从数据库还原模型时,将应用适当的数据.当将模型应用于构造函数中的数据上下文时,文本框的datacontext包含正确的数据,并且其属性应按应有的方式分配.问题是用户界面无法反映这一点.

When I save the model to the database, the proper data gets saved from the textbox. When I restore the model from the database, the proper data gets applied. When the model is applied to the data context within my constructor, the textbox's datacontext contains the correct data and it's properties are assigned as they should be. The issue is the UI does not reflect this.

XAML

<Window.DataContext>
    <models:UserPreferences />
</Window.DataContext>

        <!-- Wrap pannel used to store the manual settings for a collection device. -->
        <StackPanel Name="OtherCollectionDevicePanel">
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Margin="10, 10, 0, 0" Text="Baud Rate" />
                <TextBox Name="BaudRateTextBox" Text="{Binding Path=SelectedCollectionDevice.BaudRate, Mode=TwoWay}" Margin="10, 10, 0, 0" MinWidth="80" ></TextBox>
            </StackPanel>
            <WrapPanel>
                <TextBlock VerticalAlignment="Center" Margin="10, 10, 0, 0" Text="Com Port" />
                <TextBox Text="{Binding Path=SelectedCollectionDevice.ComPort, Mode=TwoWay}" Margin="10, 10, 0, 0" MinWidth="80" ></TextBox>
            </WrapPanel>
            <WrapPanel>
                <TextBlock VerticalAlignment="Center" Margin="10, 10, 0, 0" Text="Data Points" />
                <TextBox Text="{Binding Path=SelectedCollectionDevice.DataPoints, Mode=TwoWay}" Margin="10, 10, 0, 0" MinWidth="80" ></TextBox>
            </WrapPanel>
            <WrapPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Margin="10, 10, 0, 0" Text="WAAS" />
                <CheckBox IsChecked="{Binding Path=SelectedCollectionDevice.WAAS, Mode=TwoWay}" Content="Enabled" Margin="20, 0, 0, 0" VerticalAlignment="Bottom"></CheckBox>
            </WrapPanel>
        </StackPanel>

模型<-数据上下文.

Model <-- Datacontext.

/// <summary>
/// Provides a series of user preferences.
/// </summary>
[Serializable]
public class UserPreferences : INotifyPropertyChanged
{
    private CollectionDevice selectedCollectionDevice;

    public UserPreferences()
    {
        this.AvailableCollectionDevices = new List<CollectionDevice>();

        var yuma1 = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 31,
            DataPoints = 1,
            Name = "Trimble Yuma 1",
            WAAS = true
        };

        var yuma2 = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 3,
            DataPoints = 1,
            Name = "Trimble Yuma 2",
            WAAS = true
        };

        var toughbook = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 3,
            DataPoints = 1,
            Name = "Panasonic Toughbook",
            WAAS = true
        };


        var other = new CollectionDevice
        {
            BaudRate = 0,
            ComPort = 0,
            DataPoints = 0,
            Name = "Other",
            WAAS = false
        };

        this.AvailableCollectionDevices.Add(yuma1);
        this.AvailableCollectionDevices.Add(yuma2);
        this.AvailableCollectionDevices.Add(toughbook);
        this.AvailableCollectionDevices.Add(other);

        this.SelectedCollectionDevice = this.AvailableCollectionDevices.First();
    }

    /// <summary>
    /// Gets or sets the GPS collection device.
    /// </summary>
    public CollectionDevice SelectedCollectionDevice
    {
        get
        {
            return selectedCollectionDevice;
        }
        set
        {
            selectedCollectionDevice = value;
            this.OnPropertyChanged("SelectedCollectionDevice");
        }
    }

    /// <summary>
    /// Gets or sets a collection of devices that can be used for collecting GPS data.
    /// </summary>
    [Ignore]
    [XmlIgnore]
    public List<CollectionDevice> AvailableCollectionDevices { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notifies objects registered to receive this event that a property value has changed.
    /// </summary>
    /// <param name="propertyName">The name of the property that was changed.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

CollectionDevice <-文本框绑定到的位置.

CollectionDevice <-- Where text box binds to.

/// <summary>
/// CollectionDevice model
/// </summary>
[Serializable]
public class CollectionDevice : INotifyPropertyChanged
{
    /// <summary>
    /// Gets or sets the COM port.
    /// </summary>
    private int comPort;

    /// <summary>
    /// Gets or sets a value indicating whether [waas].
    /// </summary>
    private bool waas;

    /// <summary>
    /// Gets or sets the data points.
    /// </summary>
    private int dataPoints;

    /// <summary>
    /// Gets or sets the baud rate.
    /// </summary>
    private int baudRate;

    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the COM port.
    /// </summary>
    public int ComPort
    {
        get
        {
            return this.comPort;
        }

        set
        {
            this.comPort= value;
            this.OnPropertyChanged("ComPort");
        }
    }

    /// <summary>
    /// Gets or sets the COM port.
    /// </summary>
    public bool WAAS
    {
        get
        {
            return this.waas;
        }

        set
        {
            this.waas = value;
            this.OnPropertyChanged("WAAS");
        }
    }

    /// <summary>
    /// Gets or sets the COM port.
    /// </summary>
    public int DataPoints
    {
        get
        {
            return this.dataPoints;
        }

        set
        {
            this.dataPoints = value;
            this.OnPropertyChanged("DataPoints");
        }
    }

    /// <summary>
    /// Gets or sets the COM port.
    /// </summary>
    public int BaudRate
    {
        get
        {
            return this.baudRate;
        }

        set
        {
            this.baudRate = value;
            this.OnPropertyChanged("BaudRate");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notifies objects registered to receive this event that a property value has changed.
    /// </summary>
    /// <param name="propertyName">The name of the property that was changed.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

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

有人可以指出我正确的方向吗?我认为问题在于我在XAML中的绑定;我找不到.我需要双向绑定,因为数据可以在模型中的应用程序生命周期内随时更改(数据库通过同步更新),并且UI需要反映这些更改,但是用户可以通过以下方式将更改应用于模型用户界面.

Can someone point me in the right direction? I assume the issue is my binding in XAML; I can't find it though. I need it to be two-way bound because the data can change at any time during the apps lifetime within the model (database is updated through syncs) and the UI needs to reflect those changes, yet the user can apply changes to the model via the UI.

更新1

我试图强制更新文本框数据绑定,但效果不佳.

I tried to force the text box databind to be updated, but that did not work as well.

BindingExpression be = this.BaudRateTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

我还尝试将UpdateSourceTrigger设置为PropertyChanged,这似乎也不能解决问题.

I also tried setting the UpdateSourceTrigger to PropertyChanged and that did not seem to resolve the problem either.

<TextBox Name="BaudRateTextBox" Text="{Binding Path=SelectedCollectionDevice.BaudRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10, 10, 0, 0" MinWidth="80" ></TextBox>

更新2

我尝试遵循一些来自Microsoft ,它似乎无法解决该问题.窗口加载时,这些值仍保持为0.从数据库还原对象的状态后,绑定未更新.绑定已绑定,因为因为当我输入数据时,数据上下文也会更新.由于某种原因,当我将其设置为双向"时,它的行为就像单向".

I tried to follow along with some documentation from Microsoft and it does not seem to fix the issue. The values still remain 0 when the window loads. The binding is not being updated after I restore the state of the object from the database. Binding is wired up though because as I enter data, the data context is updated. For some reason, it's acting like One-Way when I have it set to Two-Way.

更新3

我试图将代码移到窗口加载的事件中,再移出构造函数,但这似乎没有帮助.我发现有趣的是,在反序列化过程中不会触发PropertyChanged事件.在这种情况下,我认为这无关紧要,因为该对象已正确地完全还原,然后无论如何我只是将其分配给数据上下文.我将数据上下文移出XAML并移到WindowLoaded中,以测试XAML是否是问题所在.结果是一样的.

I tried to move the code into the window loaded event and out of the constructor but that did not appear to help. Something I found interesting is that the PropertyChanged event does not get fired during the deserialization process. I don't think it matters in this case because the object is fully restored properly and then I just assign it to the data context anyway. I moved the data context out of the XAML and into the WindowLoaded in order to test if the XAML was the problem. The result was the same.

private void WindowLoaded(object sender, RoutedEventArgs e)
{
    // Restore our preferences state.
    var preferences = new UserPreferenceCommands();
    Models.UserPreferences viewModel = new Models.UserPreferences();

    // Set up the event handler before we deserialize.
    viewModel.PropertyChanged += viewModel_PropertyChanged;
    preferences.LoadPreferencesCommand.Execute(viewModel);

    // At this point, viewModel is a valid object. All properties are set correctly.
    viewModel = preferences.Results;

    // After this step, the UI still shows 0's in all of the text boxs. Even though the values are not zero.
    this.DataContext = viewModel;
}

// NEVER gets fired from within the WindowLoaded event.
void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    MessageBox.Show("Property changed!");
}

// This changes the model properties and is immediately reflected in the UI. Why does this not happen within the WindowLoaded event?
private void TestButtonClickEvent(object sender, RoutedEventArgs e)
{
    var context = this.DataContext as Models.UserPreferences;
    context.SelectedCollectionDevice.ComPort = 1536;
}

更新4-已确定问题

我已经确定了问题,但仍需要解决.数据绑定的全部目的是使我不必执行此手动分配.我的INotify实现有问题吗?

I have identified the problem, but still need a resolution. The whole point of data binding is so that I don't have to perform this manual assignment. Is there something wrong with my INotify implementations?

private void WindowLoaded(object sender, RoutedEventArgs e)
{
    // Restore our preferences state.
    var preferences = new UserPreferenceCommands();
    Models.UserPreferences viewModel = new Models.UserPreferences();

    // Set up the event handler before we deserialize.
    viewModel.PropertyChanged += viewModel_PropertyChanged;
    preferences.LoadPreferencesCommand.Execute(viewModel);

    // At this point, viewModel is a valid object. All properties are set correctly.
    viewModel = preferences.Results;

    // After this step, the UI still shows 0's in all of the text boxs. Even though the values are not zero.
    this.DataContext = viewModel;

    // SOLUTION: - Setting the actual property causes the UI to be reflected when the window is initialized; setting the actual data context does not. Why? Also note that I set this property and my PropertyChanged event handler still does not fire.
    ((Models.UserPreferences) DataContext).SelectedCollectionDevice = viewModel.SelectedCollectionDevice;

}

推荐答案

好的,我能够确定问题并将其解决.事实证明是导致这种情况的一些原因.

Alright, I was able to determine the problem and get it resolved. It turned out to be a compilation of things causing this.

首先,我的模特.

用户首选项<-MainWindow是与此绑定的数据.

UserPreferences <-- MainWindow is data bound to this.

[Serializable]
public class UserPreferences : INotifyPropertyChanged
{
    private CollectionDevice selectedCollectionDevice;

    public UserPreferences()
    {
        this.AvailableCollectionDevices = new List<CollectionDevice>();

        var yuma1 = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 31,
            DataPoints = 1,
            Name = "Trimble Yuma 1",
            WAAS = true
        };

        var yuma2 = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 3,
            DataPoints = 1,
            Name = "Trimble Yuma 2",
            WAAS = true
        };

        var toughbook = new CollectionDevice
        {
            BaudRate = 4800,
            ComPort = 3,
            DataPoints = 1,
            Name = "Panasonic Toughbook",
            WAAS = true
        };


        var other = new CollectionDevice
        {
            BaudRate = 0,
            ComPort = 0,
            DataPoints = 0,
            Name = "Other",
            WAAS = false
        };

        this.AvailableCollectionDevices.Add(yuma1);
        this.AvailableCollectionDevices.Add(yuma2);
        this.AvailableCollectionDevices.Add(toughbook);
        this.AvailableCollectionDevices.Add(other);

        this.SelectedCollectionDevice = this.AvailableCollectionDevices.First();
    }

    /// <summary>
    /// Gets or sets the GPS collection device.
    /// </summary>
    public CollectionDevice SelectedCollectionDevice
    {
        get
        {
            return selectedCollectionDevice;
        }
        set
        {
            selectedCollectionDevice = value;

            if (selectedCollectionDevice.Name == "Other")
            {
                this.AvailableCollectionDevices[3] = value;
            }

            this.OnPropertyChanged("SelectedCollectionDevice");
        }
    }

    /// <summary>
    /// Gets or sets a collection of devices that can be used for collecting GPS data.
    /// </summary>
    [Ignore]
    [XmlIgnore]
    public List<CollectionDevice> AvailableCollectionDevices { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Notifies objects registered to receive this event that a property value has changed.
    /// </summary>
    /// <param name="propertyName">The name of the property that was changed.</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

SelectedCollectionDevice的设置器中,我不是要查看所选设备是否为 other .所有其他设备(yuma1,panasonic等)都具有从未更改过的预定属性值.当用户选择其他"时,将显示文本框,他们可以手动输入数据.问题是,在窗口加载期间从数据库中还原了手动输入的数据时,我没有将SelectedCollectionDevice中的自定义数据分配给集合中的相应对象.

In the setter for the SelectedCollectionDevice I was not looking to see if the selected device was other. All of the other devices (yuma1, panasonic etc) have pre-determined property values that are never changed. When the user selects "Other" the textbox's are displayed and they can manually enter the data. The problem was that when the manually entered data was restored from the database during the window loading, I was not assigning the custom data in SelectedCollectionDevice to the corresponding object in the collection.

在窗口加载期间,Combobox.SelectedItem被设置为SelectedCollectionDevice的索引. Combobox.ItemsSource被设置为AvailableCollectionDevices集合.

During window load, the Combobox.SelectedItem was set to the index of the SelectedCollectionDevice. The Combobox.ItemsSource was set to the AvailableCollectionDevices collection.

this.CollectionDevice.SelectedIndex = 
    viewModel.AvailableCollectionDevices.IndexOf(
        viewModel.AvailableCollectionDevices.FirstOrDefault(
            acd => acd.Name == viewModel.SelectedCollectionDevice.Name));

执行上述代码时,组合框将默认对象从其数据源中拉出,该对象的所有值均设置为零.在组合框的SelectionChanged事件中,我将数据上下文SelectedCollectionDevice分配给与组合框关联的归零项目.

When the above code is executed, the combo box pulls the default object from its data source, which has all of the values set to zero. Within the combo box's SelectionChanged event I assigned the Data Context SelectedCollectionDevice to the zero'd out item associated with the combo box.

private void CollectionDeviceSelected(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count > 0 && e.AddedItems[0] is CollectionDevice)
    {
        // Assign the view models SelectedCollectionDevice to the device selected in the combo box.
        var device = e.AddedItems[0] as CollectionDevice;
        ((Models.UserPreferences)this.DataContext).SelectedCollectionDevice = device;

        // Check if Other is selected. If so, we have to present additional options.
        if (device.Name == "Other")
        {
            OtherCollectionDevicePanel.Visibility = Visibility.Visible;
        }
        else if (OtherCollectionDevicePanel.Visibility == Visibility.Visible)
        {
            OtherCollectionDevicePanel.Visibility = Visibility.Collapsed;
        }
    }
}

长话短说,我将上面的代码添加到了SelectedCollectionDevice的设置器中,以将值应用于AvailableCollectionDevices List<>.这样,当组合框选择了其他"值时,它将使用正确的数据从集合中提取该值.在反序列化期间,我只是反序列化SelectedCollectionDevice而不是List<>,这就是为什么在第一次加载窗口时总是重写数据的原因.

So long story short, I added the code above in the setter for the SelectedCollectionDevice to apply the value to the AvailableCollectionDevices List<>. This way, when the combo box has the "Other" value selected, it pulls the value from the collection with the correct data. During deserialization, I am just deserializing the SelectedCollectionDevice and not the List<> which is why the data was always being overwrote when the window first loaded.

这也解释了为什么使用本地viewModel.SelectedCollectionDevice重新分配数据上下文SelectedCollectionDevice属性的原因.我正在替换与组合框关联的归零对象,该组合框已在SelectionChanged事件期间设置了数据上下文.我无法在XAML中设置DataContext并删除手动分配.

This also explains why re-assigning the the data context SelectedCollectionDevice property with the local viewModel.SelectedCollectionDevice was working. I was replacing the zero'd out object associated with the combo box, which had set the data context during the SelectionChanged event. I am not able to set the DataContext in the XAML and remove the manual assignment.

感谢所有帮助,它帮助我缩小了调试范围,直到我终于解决了问题.非常感谢!

Thanks for all of the help, it helped me narrow down my debugging until I finally resolved the issue. Much appreciated!

这篇关于WPF双向绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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