Hi,
As per my requirement, I need to load the rows and columns dynamically.
So I used 2 classes one is to get the Column name and type ; and another class to get row information.
I took data table and created the Columns as ColumnInfo class and also updated rows info[From RowsInfo class].
And depends upon Type info from ColumnInfo class(like,TextBox,ComboBox,TextBlock) I created different styles in Xaml respectively.
Later I binded to DataTable to XamDatagrid.
I used below style for one of Field through code behind
<Style x:Key="InCellStyle" TargetType="{x:Type igDP:CellValuePresenter}" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}" > <Border CornerRadius="2" Name="MainBorder" BorderThickness="2" BorderBrush="{Path = BorderColor}"> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="TextBlock" Text="{Binding Path= Value, RelativeResource={RelativeResource Mode=TemplatedParent} }" /> </StackPanel></Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
Once i applied above style, I am able to see the Data in cell but not border Color.
I observered following error in output window of Visual studio
>>
System.Windows.Data Warning: 40 : BindingExpression path error: 'BorderColor' property not found on 'object' ''DataRecord' (HashCode=5604335)'. BindingExpression:Path=BorderColor; DataItem='DataRecord' (HashCode=5604335); target element is 'Border' (Name=''); target property is 'BorderBrush' (type 'Brush')
<<
Could you please let me , how to resolve the above issue and get the border for specific cell.
Thank you,
Chandra
Could you please help me to resolve the issue...
Thanks,
Hello Chandra,
This binding error is expected since the DataContext of the Border is DataRecord and it doesn't have a BorderColor Property, I can suggest you chnage the Binding depending on to which item the BorderColor Property belongs.
Hope this helps you.
Hi Stefan,
Could you please help me to resolve the issue.
I have created a sample project for you with the functionality you want. Basically I added a Brush Property to the Window and bind the Border in the CellValuePresenter to it. Also I can say that if you want to be able to dynamically change this Property you should implement INotifyPropertyChange in the class to which this Property belongs. Here you can read how to implement this interface:
http://msdn.microsoft.com/en-us/library/ms229614.aspx
Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thanks for your reply.
As per your suggested solution, BorderBrush of Border as "Brushes.PeachPath" and its applicable for all cell values.
Please consider the below requirement
I am binding Xamdatagrid to DataTable
In my case, DataTable columns are Name, Description,SRLoLimit,Status,Value
c1 = new ColumnInfo("Name", DisplayType.TextBlock, new CellInfo(Brushes.Purple),false);
c2 = new ColumnInfo("Description", DisplayType.TextBlock);
c3 = new ColumnInfo("SRLoLimit", DisplayType.Button);
c4 = new ColumnInfo("Status", DisplayType.ComboBox, MyProperty);
c5 = new ColumnInfo("Value", DisplayType.TextBox);
foreach (ColumnInfo item in Columns) { dt.Columns.Add(item.Name); }
And 2nd Parameter for ColumnInfo is UI Type. and 3rd parameter is BorderColor
Depends on UI type I am applying style for particular field.
And I am updating DataRows information from RowInfo class as below
RowInfo row2 = new RowInfo();
row2.rowData.Add("CV002");
row2.rowData.Add("Sample Description CV002");
row2.rowData.Add("0");
row2.rowData.Add("InActive");
row2.rowData.Add("200");
rows.Add(row2);
foreach (RowInfo rowItem in Rows) {
dr = dt.NewRow();
for (int i = 0; i < Columns.Count; i++)
{ dr[Columns[i].Name] = rowItem.rowData[i];
}
dt.Rows.Add(dr);
Consider, I want to apply the BorderColor property to BorderBrush and the BorderColor value is getting from ColumnInfo.CellInfo and it should be applicable only to value "CV002".
My Style is as below:
<Style x:Key="TextBlockCellStyle" TargetType="{x:Type igDP:CellValuePresenter}" > <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border CornerRadius="2" Name="MainBorder" BorderThickness="2" BorderBrush="{Binding Path=DataItem[BorderColor]}"> <StackPanel Orientation="Horizontal"> <TextBlock x:Name="TextBlock" Text="{TemplateBinding Value}" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter></Style>
So Could you please let me how can I achieve the above mentioned in Style of CellValuePresenter.
Thanks in advance,
Regards,
I have been looking into your question and I can suggest you use the folowing Style instead of yours:
<Style x:Key="TextBlockCellStyle" TargetType="{x:Type igDP:CellValuePresenter}" > <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Value}" Value="CV002"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border CornerRadius="2" Name="MainBorder" BorderThickness="2" BorderBrush="{Binding Path=DataItem[BorderColor]}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path= Value, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style>
where by using a DataTrigger the tempalte is set only to the Cell with "CV002" value.
Your style worked for the Value ="CV002".
But actually, in my case
row2.rowData.Add("CV002",ErrorType.Warning);
RowInfo row3 = new RowInfo();
row3.rowData.Add("CV003");
row3.rowData.Add("Sample Description CV003");
row3.rowData.Add("0");
row3.rowData.Add("InActive");
row3.rowData.Add("200",ErrorType.Warning);
rows.Add(row3);
In this case, I have 2 rows of Data
In the 1st row, I have a CV002,"Sample Description CV002",0,"InActive","200".
And 1st cell has CV002 value with ErrorType.Warning
In the SecondRow, I have CV003,"Sample Description CV003","0","InActive","200"
And the value 200 with ErrorType.Warning in 2nd Row.
In my case, I need to apply for BorderColor to specific cells which has ErrorType.Warning
i.e CV002 in 1st row and 200 in 2nd row.
But How can i apply style in the above case.
You can use the following Style:
<Style x:Key="TextBlockCellStyle" TargetType="{x:Type igDP:CellValuePresenter}" > <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=Value}" Value="CV002"/> <Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=Record.DataItem.ErrorType}" Value="Warning"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border CornerRadius="2" Name="MainBorder" BorderThickness="2" BorderBrush="{Binding Path=DataItem[BorderColor]}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path= Value, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </MultiDataTrigger.Setters> </MultiDataTrigger> </Style.Triggers> </Style>
which uses a MultiDataTrigger and it will color the Cell if its value is "CV002" and if the DataItem's ErrorType is "Warning".
As per your suggestions,
I added new Column to DataTable and hide the field and added to fieldLayout as below
dt.Columns.Add("ErrorType"); Field field1 = new Field("ErrorType"); field1.Visibility = Visibility.Hidden;
fieldLayout.Fields.Add(field1);
And I applied below style to CellValuePresenter
<Style x:Key="TextBlockCellStyle" TargetType="{x:Type igDP:CellValuePresenter}" > <!--<Setter Property="Background" Value="red"/> <Setter Property="Value" Value="{Binding Path=DataItem[BorderColor],Converter={StaticResource testconv}}"/>--> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Border CornerRadius="2" Name="MainBorder" BorderThickness="2" > <StackPanel Orientation="Horizontal"> <TextBlock x:Name="TextBlock" Text="{Binding Path=Value, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> <Image x:Name="Image" Source="{Binding Converter={StaticResource testImage}}" /> </StackPanel> </Border> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding Path=DataItem.ErrorType}" Value="Warning">
<Setter TargetName="MainBorder" Property="BorderBrush" Value="Red" />
</DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
In this case, its not applicable for specific cell, Whether I need to add any extra condition to this?
I am attaching the screenshot for your reference.
Please help me out this issue
Thanks for your reply and suggestion..
Could you please provide me the sample solution, which which can achieve the above requirement.
Thanks in advance.
I have been looking into your data and I noticed that the XamDataGrid’s DataSource is a DataTable, which is created based on the Rows collection items, where the ErrorType Property belongs to, so my suggestions is to add one more column into the DataTable with this information (ErrorType) and if you don’t want to show it, you can define a FieldLayout for the XamDataGrid and hide that Field. This way you can easily make a MultiTrigger to show red border when the value is CV002 and the ErrorType is Warning.
I saw Yanko's reply and in her reply
if ((value as DataRecord).Cells[0].Value.ToString() == "CV002" && (value as DataRecord).Cells[0].Record.Index ==1)
{
return Brushes.Orange;
else if ((value as DataRecord).Cells[0].Value.ToString() == "CV002" && (value as DataRecord).Cells[0].Record.Index == 2)
return Brushes.Pink;
In this case, CV002 and Index values are static.
But in my case, RowData is dynamic.So i cant apply the above condition to apply borderColor.
I need to apply BorderColor and Image depends upon CellData.ErrorType.Warning..
Otherthan Yanko's approach..is there any other approach ?