TwoWay绑定不更新目标 - Metro应用程序 [英] TwoWay Binding Not Updating Target - Metro App

查看:93
本文介绍了TwoWay绑定不更新目标 - Metro应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2012和Windows 8 SDK构建Metro应用程序。在应用程序中,我有这个类(具有相应的结构)

  //工具的参数数据结构
public struct ToolParameter
{
public string Title {get;组; }
public Object Value {get;组; }
public string描述{get;组;
}
//将用于在手机上执行某项操作的工具
public class Tool
{
public string Title {get;组; }
public ObservableCollection< ToolParameter>参数{get;组; }
public string描述{get;组; }
}

在应用程序的某个页面上,我绑定一个类的实例到页面的dataContext

  this.DataContext = currentTool; 

在页面上,我显示有关应用程序的各种信息,包括我想要的参数可以在页面上编辑。因此,我正在使用一个TextBox来显示参数,以便它被编辑,并将其绑定到ToolParameter结构的Value成员。

 < TextBox x:Name =ParameterValueFontSize =15Text ={Binding Value,Mode = TwoWay}TextWrapping =Wrap/> 

不幸的是,当一个TextBox绑定到一个值时,它不会更新,直到它不再具有一个焦点,所以我添加了一个按钮,用户可以点击它将更新参数(并从TextBox更改焦点)。不幸的是,点击按钮,虽然焦点改变了,但是currentTool变量中的参数的值从不改变。有没有什么关于数据绑定我失踪了?可能是TextBox的父类名为ParameterValue(参数都是ListView的一部分)必须是两种方式?

解决方案

从我可以看到,您的TextBox绑定到,它是 ToolParameter 类的属性。该页面的DataContext类型为工具。工具包含参数,它是ToolParameter对象的集合。因此,TextBox需要在ItemsCollection中,ItemSource设置为绑定到Parameters属性。



示例:

 < StackPanel> 
< TextBlock Text ={Binding Title}/>
< TextBlock Text ={Binding Description}/>

<! - 显示ListBox,但可以是任何ItemsControl - >
< ListBox ItemsSource ={Binding Parameters}>
< ListBox.ItemTemplate>
< DataTemplate>
< TextBox Text ={Binding Value}/>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
< / StackPanel>

还要确保你的课程工具 ToolParameter 实现INotifyPropertyChanged,您的属性的设置程序将触发PropertyChanged事件



更新 :添加对于评论太大的信息



这个应该有助于理解绑定中的Source / Target。对于您的TextBox,绑定的源是Value属性,Target是TextBox的TextProperty。当源更新时,文本将在TextBox内更新。如果您的TextBox的TextProperty更改,那么它将更新您的对象的Value属性(提供的模式设置为TwoWay)。你是工具,但不会更新,也不会工具类的Parameters属性。如果您希望在ToolParameter的属性更新时更新工具对象,则需要订阅添加到Parameters集合的每个ToolParameter对象的PropertyChanged事件。


I'm building a Metro App using VS 2012 and the Windows 8 SDK. In the app, I have this class (with a corresponding struct)

// Parameter data structure for tools
public struct ToolParameter
{
    public string Title { get; set; }
    public Object Value { get; set; }
    public string Description { get; set; }
}
// Tool that will be used to execute something on phone
public class Tool
{
    public string Title{ get; set; }
    public ObservableCollection<ToolParameter> Parameters { get; set; }
    public string Description { get; set; }
}

On a certain page in the app, I bind an instance of the class to the dataContext of the page

this.DataContext = currentTool;

On the page, I display various information about the app, including the parameters, which I want to make editable on the page. Because of this, I'm using a TextBox to display the parameters so that it can be edited, and binding it to the "Value" member of the ToolParameter struct.

<TextBox x:Name="ParameterValue" FontSize="15" Text="{Binding Value, Mode=TwoWay}"     TextWrapping="Wrap"/>

Unfortunately, when a TextBox is bound to a value, it doesn't update until it no longer has a focus, so I added a button that the user can click that will update the parameters (and change focus from the TextBox). Unfortunately, upon clicking of the button, though the focus changes, the values of the parameter in the currentTool variable is never changed. Is there something about data binding that I am missing? Might it be that the parent of the TextBox named ParameterValue (the parameters are all part of a ListView) has to be two way as well?

解决方案

From what I can see, youre TextBox is binding to Value which is a property of the ToolParameter class. The DataContext for the page is of type Tool. Tool contains Parameters which is a collection of ToolParameter objects. So, the TextBox needs to be within an ItemsCollection that has the ItemsSource set to bind to the Parameters property.

Example:

<StackPanel>
    <TextBlock Text="{Binding Title}"/>
    <TextBlock Text="{Binding Description}"/>

    <!-- showing a ListBox, but can be any ItemsControl -->
    <ListBox ItemsSource="{Binding Parameters}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Value}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

Also make sure that your classes Tool and ToolParameter implement INotifyPropertyChanged and that the setter for your properties fire the PropertyChanged event

UPDATE: Adding info that was too large for a comment

This should help understand Source/Target in bindings. For your TextBox, the source of the binding is the Value property and the Target is the TextProperty of the TextBox. When the source updates, the Text will update within the TextBox. If you the TextProperty of the TextBox changes, then it will update the Value property of your object (provided mode is set to TwoWay). You're tool however will NOT update and neither will the Parameters property of the Tool class. If you wish to update the tool object when a property of a ToolParameter updates, then you will need to subscribe to the PropertyChanged event of each ToolParameter object that gets added to the Parameters collection.

这篇关于TwoWay绑定不更新目标 - Metro应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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