开发者

Binding to instance of ItemsSource

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 ne开发者_运维技巧eds 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.


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)


MyProperty="{Binding Converter={StaticResource ObjectToDerivedValueConverter}

That should do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜