views:

39

answers:

2

Hello everyone,

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.

+1  A: 

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

Wouter Janssens - Xelos bvba
The Listview datacontext is a Datatable, the static filed is only the column name that I have specified in a Class.My point is not to use the column name itself but a const property from a class that is the column name.I'm able to do <TextBlock Text="{Binding {x:Static C:Values.FieldCode}}" /> outside the ItemTemplate. so I think the problem is the binding and the acess to the static field inside the ItemTemplate. Any Ideas?
Paulo
Do I understand you correctly if I think that you would like to set the path of the binding to the static property that references the columnname instead of putting the columnname in code as a string?
Wouter Janssens - Xelos bvba
Yes that's wright
Paulo
Is EDIT1 of my answer helping you with it or do you need something else.
Wouter Janssens - Xelos bvba
+1  A: 

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}}
John Bowen