Hello again,
Ok, so i have the following problem:
I have a xamdatagrid linked to a datatable that is dynamically created. I store the index of some columns and after that, depending on the value of each cell, I insert a picture.
I have the following code and trigger, pretty simple:
private void dg_InitializeRecord(object sender, InitializeRecordEventArgs e)
{
e.Record.FieldLayout.Fields[myDynamicIndex].Settings.CellValuePresenterStyle = dg.FindResource("st") as Style;
}
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Cells[ ? MY PROBLEM ? ].Value, Mode=OneWay}" Value="1">
<Setter Property="Visibility" TargetName="green" Value="Hidden"/>
<Setter Property="Visibility" TargetName="red" Value="Visible"/>
</DataTrigger>
</ControlTemplate.Triggers>
As you can see, i cannot get the index of the cell in xaml, as it is dynamically set in my .cs code. I tried the following but it didn't work
Binding="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type igDP:Cell}}}"
Binding="{Binding Value,
RelativeSource={RelativeSource AncestorType={x:Type igDP:Cell}}}"
Can anyone help?
Actually I mean something like this, but I probably got carried away and did not finish this.
Glad it works now.
However, this totally works:
private void dg_FieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e)
e.FieldLayout.Fields[myIndex].Settings.CellValuePresenterStyle = dg.FindResource("st") as Style;
and the datatrigger
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value}" Value="1">
Ok, thank you very much, this issue is now solved!
well, the line
if (this.MyIndex == e.FieldLayout.Fields[0].Index) will never work unless Myindex is 0... because you compare it to Fields[0].Index which will always retrun 0...
Well, I am not sure this could be done in the xaml. To do this, I handle the FieldLayoutInitialized event and apply the style only to that field, which index is equal to myIndex that is dynamic:
if (this.MyIndex == e.FieldLayout.Fields[0].Index)
{e.FieldLayout.Fields[0].Settings.CellValuePresenterStyle = dg.Resources["st"] as Style;}
Doing this, only that field will have this style and the data trigger looks like:
<DataTrigger Binding="{Binding Path=Value, RelativeSource={RelativeSource Self}}" Value="name1">
Hello and sorry for the late reply, I was out of town.
I have seen your code, but you still have the same problem: <DataTrigger Binding="{Binding Path=Cells[0].Value}" Value="name1">. You link the datatrigger to cells[0]. I need to link it to cells[myIndex].
That I do not know how to do....
I'm trying now to access the cellvaluepresenter's tag and store my index there, like you said: RelativeSource={RelativeSource TemplatedParent}, Path=Tag.
So far no luck, i;ll keep trying a bit more and post my results