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?
Hello,
The cells collection has both int(index) and string(name) indexators. Is it possible in your scenario that you know the name of the field? Like this:
DataTrigger Binding="{Binding Path=Cells[nameColumn].Value, Mode=OneWay}" Value="1">
Unfortunately no.. The columns in my dataTable are generated via some imputs that come from a windows form.. Is there no way of retrieving the object that is linked to the dataTrigger in my xaml code? Because then i would cast it to a cell.. as i said before, i tried this, but it didn't work : {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type xcdg:Cell}}
hello,
sorry for the late reply, i had to get some hours of sleep.
ok, here's the entire xaml an cs.
<Grid>
<igDP:XamDataGrid Name="dg" RecordAdded="dg_RecordAdded">
<igDP:XamDataGrid.Resources>
<Style x:Key="st" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<StackPanel x:Name="stackPanel" Orientation="Horizontal">
<Grid Margin="5,0,0,0" x:Name="grid" Width="Auto" Height="20">
<Grid Width="Auto" Height="20" x:Name="green" Visibility="Visible" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image Source="d:\\cell_green.png" Height="20" Width="Auto" />
</Grid>
<Image x:Name="red" Source="d:\\cell_red.png" Height="20" Width="Auto" Visibility="Hidden" />
<TextBlock Width="Auto" Height="Auto" Text="{TemplateBinding Content}" TextWrapping="Wrap" HorizontalAlignment="Center" Margin="5,0,0,0" VerticalAlignment="Center" Foreground="#FF000000" x:Name="textBlock"/>
</StackPanel>
<DataTrigger Binding="{Binding Path=Cells[1].Value}" Value="1">
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</igDP:XamDataGrid.Resources>
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings AllowEdit="False"/>
</igDP:XamDataGrid.FieldSettings>
</igDP:XamDataGrid>
cs:
public partial class GridDispRezNoua : UserControl
public GridDispRezNoua()
InitializeComponent();
public void LoadData(DataTable dt)
dg.DataSource = dt.AsEnumerable();
public int myIndex = 0;
private void dg_RecordAdded(object sender, RecordAddedEventArgs e)
CellValuePresenter.FromCell(e.Record.ParentDataRecord.Cells[myIndex]).Tag = e.Record.ParentDataRecord.Cells[myIndex].Value.ToString();
e.Record.FieldLayout.Fields[myIndex].Settings.CellValuePresenterStyle = dg.FindResource("st") as Style;
The WPF UserControl is added in another form intro a host element, myIndex is set and LoadData is called also from there.
I changed the event to recordAdded, but this one is too late, doesn't crash but also doesn't even apply the style. I still can't figure out the right event to set the tag and style.
Thanks for helping
After looking throught the code, I noticed couple of things.
First is that you are using RecordAdded event, but you have set AllowEdit to false. This event only fires when you add a record through the XamDataGrid's Add Record. You can see this in my application. So in your scenario, this event will never fire.
Secondly, I see that that the style you want to apply is actually for all the records. You do not need to apply that to all the records, but just to a field. So, there are couple of ways to do that:
1). use the FieldLayoutInitialized event (see the attached sample) and apply the style to the particular field.
2). define the FieldLayout in xaml ( and fields ) and apply the style there. As you pass an index to that UserControl, I believe you will go with option 1.
I modified your code, so that the DataTrigger will fine when the name is "name1", so click the button to add new Record.
Please let me know if you have any questions on this.
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
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">
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...
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!