简体   繁体   English

如何在c#wpf背后的代码中读取图像x:name

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

I have a Datagrid. 我有一个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”,并在需要时通过使用此属性显示该图像

image1.Visibility = Visibility.Hidden;

and image1.Visibility = Visibility.Visible; 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. 但问题是,我无法在后面的代码中读取图像控件的“ image1”名称来完成此操作。
Can anybody help me what is the best way to do this and how to read the name in the code behind from datagrid. 谁能帮我实现此目的的最佳方法是什么,以及如何从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. 下面的示例将帮助您找到子控件及其在Datagrid中的价值。 http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-find-control-and-its-value-form-datagrid-in-wpf/ 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 您可以使用Converter来解决此问题

In your resources section of the XAML 在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();
}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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