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?
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,
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
Even if the tag is not set, I believe there should be no error. Another reason for this might be the event (InitializeRecord is too early for the CellValuePresenter to be created). Do you have a sample project that reproduces this issue so that we can look into it?
Just one problem with it: it returns the object reference not set to an instance of an object error when i link it with {Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag} ..
any thoughts on what may be wrong here? (i've been killing myself with it all day so maybe a simple answer will work:) )
THANK YOU!!!
God i was searching for that all day...
Thank you very much and may I say you guys are doing a great job here;)