绑定到 ItemsSource 的实例

发布于 2024-12-06 18:40:30 字数 1646 浏览 0 评论 0 原文

我在 WPF 用户控件上有一个 ListBox,定义为

<ListBox Grid.Row="1" ScrollViewer.CanContentScroll="False" Background="#00000000" BorderThickness="0" ItemsSource="{Binding BuyItNowOptions}"
                                           ItemTemplate="{DynamicResource BuyItNowDataTemplate}" IsSynchronizedWithCurrentItem="True"
                                           Style="{DynamicResource InheritEmptyListStyle}" SelectedItem="{Binding SelectedResearch}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}"/>

BuyItNowOptions 是 ViewModel 上的一个公共属性,其类型为 ObservableCollection

在 BuyItNowDataTemplate 中,我有一个标签,需要在显示价格之前执行一些逻辑。

<Label Padding="1" HorizontalContentAlignment="Stretch" Grid.Column="2" Grid.Row="2" Margin="1">
                    <TextBlock Text="{Binding ExchangePrice, StringFormat=C}"
                        Visibility="{Binding ReturnRequired, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                </Label>

这里的绑定表明它将使用它所在的 AutoResearchProxy 实例的 ExchangePrice 属性,例如 BuyItNowOptions[CurrentIndex].ExchangePrice。

我想知道是否可以以引用 AutoResearchProxy 的整个实例的方式创建绑定,以便我可以将其传递给转换器并操作 AutoResearchProxy 的多个属性并返回计算出的价格?

我想象我的转换器看起来像这样。

public class PriceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,        System.Globalization.CultureInfo culture)
{
  if (value is AutoResearchProxy)
  {
    var research = value as AutoResearchProxy;
    //Some logic to figure out actual price

  }
  else
    return String.Empty;
}

希望这是有道理的。

I have a ListBox on a WPF User Control that is defined as

<ListBox Grid.Row="1" ScrollViewer.CanContentScroll="False" Background="#00000000" BorderThickness="0" ItemsSource="{Binding BuyItNowOptions}"
                                           ItemTemplate="{DynamicResource BuyItNowDataTemplate}" IsSynchronizedWithCurrentItem="True"
                                           Style="{DynamicResource InheritEmptyListStyle}" SelectedItem="{Binding SelectedResearch}" ItemContainerStyle="{DynamicResource ListBoxItemStyle}"/>

BuyItNowOptions is a public property on the ViewModel that is of type ObservableCollection

In the BuyItNowDataTemplate I have a label that needs to have some logic performed before displaying a price.

<Label Padding="1" HorizontalContentAlignment="Stretch" Grid.Column="2" Grid.Row="2" Margin="1">
                    <TextBlock Text="{Binding ExchangePrice, StringFormat=C}"
                        Visibility="{Binding ReturnRequired, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                </Label>

The binding here indicates that it will use the ExchangePrice property of the instance of AutoResearchProxy that it is on like BuyItNowOptions[CurrentIndex].ExchangePrice.

What I would like to know is it possible to create the binding in such a way that it references the whole instance of the AutoResearchProxy so that I can pass it to a converter and manipulate several properties of the AutoResearchProxy and return a calculated price?

I would envision my converter looking like this.

public class PriceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,        System.Globalization.CultureInfo culture)
{
  if (value is AutoResearchProxy)
  {
    var research = value as AutoResearchProxy;
    //Some logic to figure out actual price

  }
  else
    return String.Empty;
}

Hopefully this makes sense.

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

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

发布评论

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

评论(2

堇年纸鸢 2024-12-13 18:40:30

您可以通过不指定 Binding .path.aspx" rel="nofollow">Path 或将其设置为 .,但是,如果存在以下任一情况,这将导致绑定不更新该对象的相关属性发生变化。

我建议您使用 MultiBinding 相反,这样您就可以定位必要的属性,并且如果其中任何一个发生更改,绑定将更新。 (有关使用示例,请参阅 MSDN 上的相应部分)

You can pass the whole datacontext-object to a Binding by not specifying a Path or by setting it to ., that however will result in the binding not updating if any of the relevant properties of that object change.

I would recommend you use a MultiBinding instead, that way you can target the necessary properties and the binding will update if any of those change. (For usage examples see the respective section on MSDN)

嗫嚅 2024-12-13 18:40:30

MyProperty="{Binding Converter={StaticResource ObjectToDerivedValueConverter}

应该可以。

MyProperty="{Binding Converter={StaticResource ObjectToDerivedValueConverter}

That should do it.

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