简体   繁体   中英

How to read the image x:name in the code behind c# wpf

I have a Datagrid. In that, I take an Image Control in the

 <DataGridTemplateColumn.HeaderTemplate>
    <DataTemplate>
        <Image name:image1 source="">
 </DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>

Now, I want Hide this Image "image1" and visible when needed by using this property

image1.Visibility = Visibility.Hidden;

and image1.Visibility = Visibility.Visible;

but problem is, I am not able to read the "image1" name of the image control in the code behind to accomplish this.
Can anybody help me what is the best way to do this and how to read the name in the code behind from datagrid.

Thanks in advance

You can't find child control directly. Below example will help you find child control and it's value from Datagrid. http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-find-control-and-its-value-form-datagrid-in-wpf/

You can use Converter for this problem

In your resources section of the XAML

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

Add in Image

Visibility="{Binding BoolValue, Converter={StaticResource BooleanToVisibilityConverter}}"

Converter

public class BooleanToVisibilityConverter : IValueConverter
{

private object GetVisibility(object value)
{
    if (!(value is bool))
        return Visibility.Collapsed;
    bool objValue = (bool)value;
    if (objValue)
    {
        return Visibility.Visible;
    }
    return Visibility.Collapsed;
}

public object Convert(object value, Type targetType, object parameter, string language)
{
    return GetVisibility(value);
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
    throw new NotImplementedException();
}


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM