开发者

wpf datagrid bug? when toggle visibility on a column with an image as headercontent

开发者 https://www.devze.com 2023-02-22 21:56 出处:网络
i have the following problem. if i use an image for a datagrid header i get an error while toggling visibility.

i have the following problem.

if i use an image for a datagrid header i get an error while toggling visibility.

<DataGridTextColumn Header="{StaticResource Image_Link}" IsReadOnly="True">

Error: Bei dem angegebenen Element handelt es sich bereits um das logische untergeordnete Element eines anderen Elements.

the only workaround i found so far is to create a ControlTemplate with the specific image foreach of my imageheadercolumns.

is this datagrid behav开发者_开发知识库iour a bug?

EDIT: Error translated by google: The specified element is already the logical child of another element.

EDIT: Sample

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    </Window.Resources>
    <Grid> 
    <Button Content="Toggle" Click="Button_Click" Height="20" Width="40" HorizontalAlignment="Left"/>
    <DataGrid Width="200">
        <DataGrid.Columns>
        <DataGridTextColumn x:Name="colImage" Header="{StaticResource AnyImage}">

        </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
    </Grid>
</Window>

ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Image x:Key="AnyImage" Source="Home.png" x:Shared="false"/>
</ResourceDictionary>

Button.Click:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.colImage.Visibility = this.colImage.Visibility == Visibility.Visible
                                       ? Visibility.Collapsed
                                       : Visibility.Visible;
    }



**EDIT: Deleted the previous answer which wasn't relevant**

You will need to put the image in headertemplate of column. working sample code

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BitmapImage x:Key="Image_Link" UriSource="test.bmp" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Content="Button" Name="button1" Width="75" Click="button1_Click" />
        <Grid Grid.Row="1">
            <DataGrid Height="200" VerticalAlignment="Top">
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="firstHeader" IsReadOnly="True">
                        <DataGridTextColumn.HeaderTemplate>
                            <DataTemplate>
                                <Image Source="{StaticResource Image_Link}"  />
                            </DataTemplate>
                        </DataGridTextColumn.HeaderTemplate>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Grid>
</Window>


private void button1_Click(object sender, RoutedEventArgs e)
{
        firstHeader.Visibility = firstHeader.Visibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
}


I'm not sure what's causing this, but you could try to explicitly create the Image control to display the image:

<DataGridTextColumn IsReadOnly="True">
    <DataGridTextColumn.Header>
        <Image Source="{StaticResource Image_Link}" />
    </DataGridTextColumn.Header>
</DataGridTextColumn>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号