1

My app structure is as follows:

MainPage.xaml has a ListView that has it's ItemSource set to a CollectionViewSource which is populated in code-behind:

MainPage.xaml

<Page.Resources>
    ...
    <CollectionViewSource x:Key="src" IsSourceGrouped="True" />
    ...
</Page.Resources>

<Grid>
    ...
    <ListView
            ItemsSource="{Binding Source={StaticResource src}}" 
            SelectionMode="None"
            ItemTemplate="{StaticResource processTemplate}"
            ItemContainerStyle="{StaticResource ListViewItemStyle}">
        <ListView.GroupStyle>
            <GroupStyle HeaderTemplate="{StaticResource groupTemplate}"/>
        </ListView.GroupStyle>
    </ListView>
    ...
</Grid>

MainPage.xaml.cs

var cvs = (CollectionViewSource)Resources["src"];
cvs.Source = groups.ToList();

Where groups is a Linq query which is grouping objects by an object property

This is all working fine to display my groups objects in a ListView successfully. Where I have an issue is inside the layout of the individual list items. The template looks like this and includes a Usercontrol defined in another file.

MainPage.xaml

<DataTemplate x:Name="processTemplate">
    <Grid>
    ...
    <TextBlock Text="{Binding Path=Process}" ... />
    <TextBlock Text="{Binding Path=Description}" ... />
    <TextBlock Text="{Binding Path=LastSuccess}" ... />                
    <Button Grid.Column="1" Grid.RowSpan="3" 
           Background="{Binding Path=Status, 
           Converter={StaticResource stbConverter}}" ... />
    <local:MinutesOverlay ... Visibility="{Binding Path=Status, 
           Converter={StaticResource stoConverter}}"
           Overdue="{Binding Path=MinutesWarning}"
           Alert="{Binding Path=MinutesAlert}"/>
        </Grid>
    </DataTemplate>

MinutesOverlay.xaml

<Grid>
    ...
    <TextBlock Text="{Binding Path=Overdue}" />
    <TextBlock Text="{Binding Path=Alert}" />
    ...
</Grid>

MinutesOverlay.xaml.cs

public sealed partial class MinutesOverlay : UserControl
{

    public MinutesOverlay()
    {
        this.InitializeComponent();
    }

    public static readonly DependencyProperty OverdueProperty = DependencyProperty.Register(
        "Overdue", typeof(int), typeof(MinutesOverlay), new PropertyMetadata(0));
    public static readonly DependencyProperty AlertProperty = DependencyProperty.Register(
        "Alert", typeof(int), typeof(MinutesOverlay), new PropertyMetadata(0));

    public int Overdue
    {
        get { return (int)GetValue(OverdueProperty); }
        set { SetValue(OverdueProperty, value); }
    }

    public int Alert
    {
        get { return (int)GetValue(AlertProperty); }
        set { SetValue(AlertProperty, value); }
    }
}

My bindings do not work and I cannot figure out how to get them to work. At present, the visibility of the MinutesOverlay control is governed by a binding which works as long as I do not set the Datacontext of the MinutesOverlay. If I do set it by doing this.Datacontext = this then the binding has no effect and the overlay is always visible(it should be collapsed most of the time).

If I set the values of Overdue and Alert without bindings in the MainPage.xaml it works fine.

3
  • Have you tried setting a name (x:Name="userControl") on your usercontrol and changed the binding to Text="{Binding Path=Alert, ElementName=userControl}"? Commented May 26, 2015 at 6:14
  • That did the trick, can you explain why? Also if you add it as an answer I can accept it Commented May 28, 2015 at 20:35
  • Added it as an answer. MSDN: "By default, bindings inherit the data context specified by the DataContext property, if one has been set. However, the ElementName property is one of the ways you can explicitly set the source of a Binding and override the inherited data context." - msdn.microsoft.com/en-us/library/… Commented May 29, 2015 at 7:34

1 Answer 1

2

Have you tried setting a name (x:Name="userControl") on your usercontrol and changed the binding to Text="{Binding Path=Alert, ElementName=userControl}"?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.