3

Please put me out of my misery:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="IsEnabled" Value="{Binding MyBoolField}" />
    </Style>
</ListView.ItemContainerStyle>

Where MyBoolField is a property available on each item in the ListView's assigned ItemsSource, and is of course of type bool.

The desired behaviour is that the IsEnabled property of each ListViewItem is bound to the MyBoolField property of the object it contains (an element from the ListView's ItemsSource). The example above however pays no attention to this binding and is always true.

If I set the value to false statically, the item becomes disabled as expected.

I can only assume this is an issue of scoping, or a restriction on the use of bindings in this particular scenario, however I am unable to find any documentation to support this.

Perhaps it is useful to know that bindings set up in the DataTemplate assigned to this ListView's ItemTemplate all work okay, so the problem is hopefully not that fundamental/stupid.

Points from the Comments

  • There is no relevant output in the debug 'Output' window.
  • Binding the MyBoolField property elsewhere works fine, as such the underlying data source is providing a Boolean value correctly and this seems to be solely an issue of binding it in this context.
jstephenson
  • 2,170
  • 14
  • 14
  • Is the class containing MyBoolField properly implementing INotifyPropertyChanged? – Kal_Torak Oct 02 '12 at 00:15
  • 2
    Check for binding errors in the Output window of Visual Studio when debugging. – Jay Oct 02 '12 at 00:39
  • @Jay No errors in the Output window. – jstephenson Oct 02 '12 at 08:07
  • @DJ Just did - seems to have had no effect. – jstephenson Oct 02 '12 at 09:57
  • Of general use may be that the getter seems never to be invoked on MyBoolField, so it would seem the Setter doesn't invoke the binding whatsoever. – jstephenson Oct 02 '12 at 09:58
  • Can you please explain the question bit more. Normally you want that if MyBoolField = false the list view item should be disabled but from your question it seems that you are trying to acheive something different. – D J Oct 02 '12 at 10:07
  • @DJ I have re-worded my question, hopefully this helps. – jstephenson Oct 02 '12 at 10:17
  • Just to make sure the style itself is working. Can you set the ToolTip, Header or any Color to a hardcoded value, which definetly must work, to see if the style is properly applied? – dowhilefor Oct 02 '12 at 10:28
  • @dowhilefor Statically defined styles do indeed take effect as expected. – jstephenson Oct 02 '12 at 10:33
  • @jstephenson ok my finaly idea is: Its not the XAML its the code. Can you show as your class, at least with the property itself? – dowhilefor Oct 02 '12 at 10:36
  • The debugger's output gives details about failed bindings - e.g. on which class the property "MyBoolField" is expected to be. If it's not the item class, try binding to RelativeSource.Self. – HDW Production Oct 02 '12 at 10:43
  • @dowhilefor Binding to MyBoolField elsewhere - for example within the ItemTemplate - works fine, so I am sure it is solely an issue of binding in this context. – jstephenson Oct 02 '12 at 10:50
  • @HDWProduction There is no indication of a problem in the debugger output. It is as if the binding is ignored entirely. – jstephenson Oct 02 '12 at 10:51

1 Answers1

0

Ok, reading this post it seems that its a known limitation of WinRT, maybe you find something useful there. One thing i would also try, because the OP in that post said something regarding that is. Using a Self binding, and use Path=DataContext.MyBoolField

Further reading, this is a limitation from Silverlight, which WinRT was build upon. So this is a workaround which should also work for WinRT

Community
  • 1
  • 1
dowhilefor
  • 10,971
  • 3
  • 28
  • 45