locked
Xaml Binding to empty collection RRS feed

  • Question

  • Hello

    I have an entity called Employee with an ObservableCollection<Address> and Address has several properties eg. Town.

    Now I'd like to bind directly to the Town value as in

    <TextBox Text="{Binding Path=Employee.Addresses[0].Town" />
    

    If Addresses is empty, I get as expected
    System.Windows.Data Error: 17 ... ArgumentOutOfRangeException

    The application continues running without problems, but an exception is an exception.

    I know, I could define in my ViewModel a new string property TownValue, verify if Addresses[0] exists and then assign that value to TownValue and set the Binding to that property.

    But I'd prefer some solution on the xaml/Binding side.

    • A new collection inheriting from ObservableCollection with an overriden indexer?
    • Converter with the collection and the index as parameters?
    • Some MarkupExtension to extend the Binding?

    Any hints?

    Friday, January 25, 2013 2:48 PM

Answers

  • You could use the Count property on the Addresses ObservableCollection for a DataTrigger on the TextBox style as follows:

    <TextBox>
        <TextBox.Style>
            <Style TargetType="TextBox">
                        <Setter Property="Text" Value="{Binding Path=Employee.Addresses[0].Town}"/>
                <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Employee.Addresses.Count}" Value="0">
                        <Setter Property="Text" Value="No items"/>
                    </DataTrigger> 
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

    • Proposed as answer by TSoftware-Old Saturday, January 26, 2013 2:13 PM
    • Marked as answer by Sheldon _Xiao Tuesday, February 5, 2013 9:54 AM
    Saturday, January 26, 2013 3:21 AM
  • Methinks you can also use a FallbackValue

    <Setter Property="Text" Value="{Binding Path=Employee.Addresses[0].Town, FallbackValue='Not found'}"/>

    I don't believe you can set this globally though.

    • Marked as answer by Sheldon _Xiao Tuesday, February 5, 2013 9:54 AM
    Wednesday, January 30, 2013 1:38 AM

All replies

  • Something like a DataTrigger: http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx

    should help you get what you want to do.  Based upon the data trigger, you can set the Text property of your TextBox.


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Friday, January 25, 2013 3:01 PM
  • You could use the Count property on the Addresses ObservableCollection for a DataTrigger on the TextBox style as follows:

    <TextBox>
        <TextBox.Style>
            <Style TargetType="TextBox">
                        <Setter Property="Text" Value="{Binding Path=Employee.Addresses[0].Town}"/>
                <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Employee.Addresses.Count}" Value="0">
                        <Setter Property="Text" Value="No items"/>
                    </DataTrigger> 
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

    • Proposed as answer by TSoftware-Old Saturday, January 26, 2013 2:13 PM
    • Marked as answer by Sheldon _Xiao Tuesday, February 5, 2013 9:54 AM
    Saturday, January 26, 2013 3:21 AM
  • OK, thanks.

    I see the direction in which I'll have to solve my problem.

    Any suggestions for a more generic solution, ie putting the DataTrigger into a generic themes file?

    Tuesday, January 29, 2013 3:18 PM
  • Methinks you can also use a FallbackValue

    <Setter Property="Text" Value="{Binding Path=Employee.Addresses[0].Town, FallbackValue='Not found'}"/>

    I don't believe you can set this globally though.

    • Marked as answer by Sheldon _Xiao Tuesday, February 5, 2013 9:54 AM
    Wednesday, January 30, 2013 1:38 AM
  • I'm not sure how you would make the Binding on the DataTrigger generic though.
    Wednesday, January 30, 2013 3:09 AM
  • I'm currently on another project, I'll test this solution at a later date.

    Thursday, January 31, 2013 6:46 AM
  • Hi Peter Moessinger,
     
    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.


    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, February 5, 2013 9:54 AM