Paulo
Paulo

Reputation: 619

Binding with Static properties in ListView ItemTemplate

I'm having some problems with WPF binding.
I have an assembly with some const properties in class Values, that correspond to columns from datatable. I want to bind the value from a column to a TextBlock using the const property to specify the column at a ListView ItemTemplate like shown in the code:

 xmlns:C="clr-namespace:WPFApplication1.Entities;assembly=WPFApplication1">
  <Grid>  
   <ListView>
    <ListView.ItemTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding {x:Static C:Values.FieldCode}}" /> /*<- Don't work*/
            /*Works like this: <TextBlock Text="{Binding [CODE]}" />*/ 
          </DataTemplate>
       </ListView.ItemTemplate>
    </ListView>
  </Grid>

If I use binding with the static property I I'm not able to show the value in the datarow but if I use the Binding like this [CODE] I'm able to show the value.

What is appening? Any clue?

Thanks in advance.

Upvotes: 4

Views: 1340

Answers (2)

Wouter Janssens
Wouter Janssens

Reputation: 1613

the italic text is not correct, please read from EDIT1:
It is not possible to bind to static properties. Binding always needs an instance of the Class. It is possible by instantiating the class as resource of in the code behind and set that class as the datacontext

EDIT1:

Add a static property of type

public static string FieldCode = "Code";
public static PropertyPath FieldCodePath = new PropertyPath(FieldCode);

Change the Binding to the binding below:

<TextBlock Text="{Binding Path={x:Static C:Values.FieldCodePath}, IsAsync=true}" />

I hope this helps

Upvotes: 1

John Bowen
John Bowen

Reputation: 24453

You need to use your static property as the Source, not the Path, which is the default attribute for Binding:

{Binding Source={x:Static C:Values.FieldCode}}

Upvotes: 3

Related Questions