使用 mode=twoway 进行数据绑定时出错

发布于 2024-12-11 12:38:52 字数 1655 浏览 0 评论 0原文

全部, 我在这里用头撞墙。我需要的很简单,而且我确信有一个简单的答案,但我似乎找不到它。 情况:我有一个 Silver light 4.0 应用程序,目前正在将字符串列表绑定到 Items 控件。在数据模板中,我有一个简单的文本框,我正在执行非常基本的绑定“{Binding}”。我需要将绑定更新为双向,以便编辑自动推回我的数据上下文。

这是更新绑定之前的项目控件:

<ItemsControl x:Name="spLiftHeader" ItemsSource="{Binding Path=WeekLabels}">
   <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
             <StackPanel x:Name="spLiftHeader" Orientation="Horizontal" />
        </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
         <DataTemplate>
              **<TextBox x:Name="txtWeekLbl" Text="{Binding}" Foreground="Black" Width="125" TextAlignment="Center"/>**
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这是绑定更改之后的项目控件:

<ItemsControl x:Name="spLiftHeader" ItemsSource="{Binding Path=WeekLabels}">
   <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
              <StackPanel x:Name="spLiftHeader" Orientation="Horizontal" />
        </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
        <DataTemplate>
            **<TextBox x:Name="txtWeekLbl" Text="{Binding Mode=TwoWay}" Foreground="Black" Width="125" TextAlignment="Center"/>**
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我只是简单地将“Mode=TwoWay”添加到绑定中。 更新后,我收到了令人惊讶的无用错误 4004 “System.Windows.Markup.XamlParseException:在“System.Windows.Data.Binding”上提供值引发异常”并且行/位置参考点正确执行我更新的绑定。 如何将双向模式添加到简单绑定中? 提前致谢。 缺口

All,
I'm pounding my head against the wall here. What I need is simple, and I'm sure there is a simple answer, but I can't seem to find it.
Situation: I have a Silver light 4.0 app and I'm currently binding a list of strings to an Items control. In the data template for that I have a simple text box that I was doing very basic Binding "{Binding}". I need to update the binding to be twoway so the edits are automatically pushed back to my datacontext.

Here is the Items control before I update the binding:

<ItemsControl x:Name="spLiftHeader" ItemsSource="{Binding Path=WeekLabels}">
   <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
             <StackPanel x:Name="spLiftHeader" Orientation="Horizontal" />
        </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
         <DataTemplate>
              **<TextBox x:Name="txtWeekLbl" Text="{Binding}" Foreground="Black" Width="125" TextAlignment="Center"/>**
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Here is the items control after the binding change:

<ItemsControl x:Name="spLiftHeader" ItemsSource="{Binding Path=WeekLabels}">
   <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
              <StackPanel x:Name="spLiftHeader" Orientation="Horizontal" />
        </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
        <DataTemplate>
            **<TextBox x:Name="txtWeekLbl" Text="{Binding Mode=TwoWay}" Foreground="Black" Width="125" TextAlignment="Center"/>**
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I've just simply added the "Mode=TwoWay" to the binding.
After updating that, I get the amazingly useless error 4004
"System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Data.Binding' threw an exception" and the Line/Position reference points right do my updated Binding.
How does one add the Two Way mode to the simple binding?
Thanks in advance.
Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-12-18 12:38:52

到整个对象(在本例中为字符串)的双向绑定对于 Silverlight 来说没有任何意义,因此抛出异常是正确的。遗憾的是,这不是一个更有用的错误消息:)

当绑定中没有 Path 时,ItemsControl 可以使用 Object.ToString() 获取值,但它将把结果存储回哪里?它无法替换字符串,因为这需要将新的字符串对象放回集合中。双向绑定是通过针对对象属性的反射来完成的。

使用包含字符串属性并显式绑定到该属性的某些新对象的列表,而不是简单的字符串列表。这将使一切变得更加容易。 (确保您的新类和属性实现 INotifyPropertyChanged)。

Two-way binding to an entire object (a string in this case) makes no sense to Silverlight so it is correct to throw an exception. Shame it is not a more useful error message :)

When there is no Path in the binding the ItemsControl can fetch a value using Object.ToString(), but where will it store the result back? It can't replace the string as that would require placing a new string object back in the collection. Two-way binding is done via reflection against a property of an object.

Instead of a simple list of strings, use a list of some new object that contains a string property and explicitly bind to that property. It will make everything a lot easier. (Make sure your new class and property implement INotifyPropertyChanged).

故人的歌 2024-12-18 12:38:52

我不是 100% 确定,但我不认为 Mode=TwoWay 应该在 TextBox 本身中设置。

如果这不起作用,请在两个上都尝试一下。但是,无论哪种情况,都不要使用 List 作为该项目控件背后的数据源。当其中一项发生更改时,列表不会触发更改事件。使用 ObservableCollection(来自 System.Collections.ObjectModel 命名空间)而不是 List

I am not 100% sure, but I don't think the the Mode=TwoWay should be set in the TextBox itself.

If that doesn't work, try it on both. However, in either case do not use List<T> as a data source behind that items control. Lists do not fire changed event when one of its items has changed. Use ObservableCollection<T> (from the System.Collections.ObjectModel namespace) instead of the List<T>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文