简体   繁体   中英

How to enable a cell in a disabled row of a WPF ListView

I have a WPF ListView who's view is set to a GridView. Rows in the list are sometimes disabled but I need one column of cells to ALWAYS be enabled. We are displaying a link in the "URL" column and the user needs the ability to click the link even if the row is disabled. Currently, if the row is disabled, the link is disabled as well.

I currently have this in XAML:

        <ListView.View>
            <GridView>



                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock
                                Text="{Binding Item.Name}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Header="Description">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock
                                Text="{Binding Item.Description}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="URL">                       
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                         <Label>
                                <Hyperlink 
                                    Command="{Binding ViewModel.OpenInstructionsCommand}">
                                    <TextBlock Text="View"  />
                                </Hyperlink>
                            </Label>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView>
        </ListView.View>

Although it is not guaranteed to work without side effects for all control types, it is possible to extend the control that you want to remain enabled to ignore the propagation of the IsEnabled property from the parent control. For an example of this with a bit more explanation, see Cedric Dussud's post here .

In this case, your code might look like:

internal class EnabledHyperlink : Hyperlink
{
    static EnabledHyperlink()
    {
        IsEnabledProperty.OverrideMetadata(typeof (EnabledHyperlink),
            new UIPropertyMetadata(true,
                IsEnabledPropertyChanged,
                CoerceIsEnabled));
    }

    private static void IsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
    {
    }

    private static object CoerceIsEnabled(DependencyObject source, object value)
    {
        return value;
    }
}

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