WPF Update Binding when Bound directly to DataContext w/ Converter

后端 未结 3 457
别那么骄傲
别那么骄傲 2020-12-31 18:45

Normally when you want a databound control to \'update,\' you use the \"PropertyChanged\" event to signal to the interface that the data has changed behind the scenes.

相关标签:
3条回答
  • 2020-12-31 19:13

    Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.

    Regards

    0 讨论(0)
  • 2020-12-31 19:15

    The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned

    public Object Self { get { return this; }}

    Then in the binding I used this property:

    <Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>

    Then when I call

    PropertyChanged(this, new PropertyChangedEventArgs("Self"))

    it works like a charm.

    Thanks all.

    0 讨论(0)
  • 2020-12-31 19:22

    I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.

    From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.

    0 讨论(0)
提交回复
热议问题