D.Young
D.Young

Reputation: 113

Binding problems in DataTrigger

In Josh Smith's article,"WPF Apps With The Model-View-ViewModel Design Pattern"

In the code from AllCustomersView.xaml:

  <DataTrigger Binding="{Binding Path=Name}" Value="True">
      <Setter TargetName="txt" Property="Text" Value="Company" />
  </DataTrigger>

I couldn't find out where the property--Name is. I thought it was in the CustomerViewModel.cs. But indeed it wasn't. How could I know the path of Name?

Upvotes: 0

Views: 1851

Answers (3)

blindmeis
blindmeis

Reputation: 22445

<DataTrigger Binding="{Binding Path=Name}" Value="True">
  <Setter TargetName="txt" Property="Text" Value="Company" />
</DataTrigger>

cause your DataTrigger is part of a groupstyle and your groupstyle is used for your listview and your listview datacontext is a collectionviewsource with a groupdescription --> the Binding Path=Name targets the properties of MS.Internal.Data.CollectionViewGroupInternal

so you can access all properties from CollectionViewGroup

btw you can use Snoop to find out more about your wpf application :)

Upvotes: 0

user1228
user1228

Reputation:

It comes from the CollectionViewSource:

<CollectionViewSource
  x:Key="CustomerGroups" 
  Source="{Binding Path=AllCustomers}">
  <CollectionViewSource.GroupDescriptions>
    <PropertyGroupDescription PropertyName="IsCompany" />
  </CollectionViewSource.GroupDescriptions>
  <CollectionViewSource.SortDescriptions>
    <scm:SortDescription PropertyName="IsCompany" Direction="Descending" />
    <scm:SortDescription PropertyName="DisplayName" Direction="Ascending" />
  </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

When you use a CVS, it takes your data (in this case, a collection of CustomerViewModel instances), and splits them into groups (in this case, based on the property IsCompany).

Each group is placed in a CollectionViewGroup. A collection of these groups is then bound to the control that will be showing the groups. So the Name property is actually found on the CollectionViewGroup, and is the value of IsCompany (that's the PropertyGroupDescription), or the string value of a boolean.

If you change the binding to something invalid

<DataTrigger Binding="{Binding Path=Derp}" Value="True">
    <Setter TargetName="txt" Property="Text" Value="Companies" />
</DataTrigger>

you'll see it stop working, and the following message show up in your output window:

System.Windows.Data Error: 39 : BindingExpression path error: 'Derp' property not found on 'object' ''CollectionViewGroupInternal' (HashCode=41413147)'. BindingExpression:Path=Derp; DataItem='CollectionViewGroupInternal' (HashCode=41413147); target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')

CollectionViewGroupInternal is the internal implementation of CollectionViewGroup that the CollectionViewSource uses to group your collection on the property IsCompany

Upvotes: 2

Dariusz Wickowski
Dariusz Wickowski

Reputation: 191

In setter there is a TargetName="txt" which is name of

      <TextBlock 
        x:Name="txt" 
        Background="{StaticResource Brush_HeaderBackground}"
        FontWeight="Bold"
        Foreground="White"
        Margin="1"
        Padding="4,2,0,2"
        Text="People" 
        />

So this binding Path is set to this control property Name

Upvotes: 0

Related Questions