Hi,
I have a unbound column in my xamdatagrid that will display button and image inside it.
Depending on the condition, the column can have image or no image (but it will always have button). The condition is known only when we start loading data in xamdatagrid.. i.e. we need to do some operation on one of the field to determine if the unbounded column will have image or not.
what is the best way to do this. For now i am doing like this:-
I am using CellValuePresenter to display button and image and I am trying to change image in RoutedCommand binded to Command property of Button.
<Style x:Key="myUnboundColumn" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"> <Button Background="White" BorderBrush="LightGray" x:Name="btn" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Command="{x:Static myns:mycls.EditCommand}"
CommandParameter="{Binding}"> <Grid> <Image HorizontalAlignment="Center" VerticalAlignment="Center" Width="16" Height="16" Source="Image.png" x:Name="imgButton"/></Grid> </Button></Grid></ControlTemplate></Setter.Value></Setter></Style>
and
<CommandBinding Command="{x:Static myns:mycls.EditCommand}"CanExecute="Edit_CanExecute" Executed="Edit_Executed"/>
So, I am trying to Access the image control inside my button control inside Edit_CanExecute method, so that i can check condition and remove the image where necessary. But i am unable to access it. what is the best way to change the source of that Image control dynamically.
thanksmiztaken
Hello miztaken,
I am really glad that you manage to resolve the issue on your side. If you still have any concerns on this let me know.
Hi Elena,
thanks for your concern.
That space was a mistake when i copied my code to the forum. I have tried both of your approaches to get image but was still getting null value so instead what i did was get the instance of button itself and create a new Image object and set its dimension and everything and then assign it as the Content of button.I am pretty sure this is not the most efficient way to do it, but for me, this was the only thing that worked.
Thank You.miztaken
I am just checking have you been able to resolve your issue? If you still need any assistance on the matter do not hesitate to ask.
Hello Miztaken,
I was looking into your snippets and I noticed that in the following line:
System.Windows.Controls.Image imageInstance= (Image)Infragistics.Windows.Utilities.GetDescendantFromName(cvp, " imgButton");
You have left one additional space before the name of the image so it can not be found.
Based on this I can suggest you use:
System.Windows.Controls.Image imageInstance = (Image)Infragistics.Windows.Utilities.GetDescendantFromName(cvp, "imgButton");
or
System.Windows.Controls.Image imageInstance = (Image)Infragistics.Windows.Utilities.GetDescendantFromType(cvp, typeof(Image), false);
Please let me know if I can provide you with any further assistance on this.
Thanks for your reply.What I am unable to understand is, the xamdatagrid is displaying the image. So, that means internally the Converter is being called on the image path provided in Source attribute.
But why am i not being able to access it inside can_execute event?
my Edit_CanExecute command looks like this:-
private void Edit_CanExecute(object sender, CanExecuteRoutedEventArgs e){
//I am passing button as parameter from xaml, this is working correctly. System.Windows.DependencyObject source = e.Parameter as System.Windows.DependencyObject;
//I am able to get instance of CellValuePresenter as wellCellValuePresenter cvp = Infragistics.Windows.Utilities.GetAncestorFromType(source, typeof(CellValuePresenter), true) as CellValuePresenter;
//But i Am getting NULL when i am trying to get instance of image inside button System.Windows.Controls.Image imageInstance= (Image)Infragistics.Windows.Utilities.GetDescendantFromName(cvp, " imgButton");
}
I have changed the commandparameter to following:-
CommandParameter="{Binding ElementName=btn}"
Why am i not getting instance of Image from cellvaluepresenter?
Do i still need to use converter even if xamdatagrid is able to disable the image correctly.
I was thinking of getting instance of image and changing is visibility.
Thanksmiztaken