簡體   English   中英

如何在c#wpf背后的代碼中讀取圖像x:name

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

我有一個Datagrid。 因此,我在

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

現在,我要隱藏此圖像“ image1”,並在需要時通過使用此屬性顯示該圖像

image1.Visibility = Visibility.Hidden;

image1.Visibility = Visibility.Visible;

但問題是,我無法在后面的代碼中讀取圖像控件的“ image1”名稱來完成此操作。
誰能幫我實現此目的的最佳方法是什么,以及如何從datagrid中讀取代碼中的名稱。

提前致謝

您無法直接找到子控件。 下面的示例將幫助您找到子控件及其在Datagrid中的價值。 http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-find-control-and-its-value-form-datagrid-in-wpf/

您可以使用Converter來解決此問題

在XAML的資源部分中

<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

在圖像中添加

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

變流器

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