简体   繁体   中英

Disable/Enable Hyperlink on runtime in WPF DevExpress Grid

I am working with DevExpress Grid control. This is the structure of my Grid:

<dxg:GridControl x:Name="grd_NoPartNumberLinesapprovedbutnotReceived" Height="600">
    <dxg:GridControl.Columns>                                                      
        <dxg:GridColumn FieldName="emo_number" Header="EMO" VisibleIndex="10" AllowEditing="False">
            <dxg:GridColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Hyperlink NavigateUri="{Binding Data.Hyperlink}" 
                                   Click="ClickonEmoHyperlink" 
                                   TargetName="_blank">

                            <TextBlock Text="{Binding Data.emo_number}" />
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </dxg:GridColumn.CellTemplate>
        </dxg:GridColumn>
    </dxg:GridControl.Columns>

    <dxg:GridControl.View>
        <dxg:TableView x:Name="vwNoPartNumberLinesapprovedbutnotReceived" AutoWidth="True" ShowGroupPanel="False" />    
    </dxg:GridControl.View>
</dxg:GridControl>

Problem

Now I want to disable Hyperlink on run time based on some condition otherwise in rest of cases it will be enable.

In this case you can use the DataTrigger :

Represents a trigger that applies property values or performs actions when the bound data meets a specified condition.

Example:

<DataTemplate>            
    <TextBlock Name="MyTextBlock" Tag="True">
        <Hyperlink Name="MyHyperlink" Click="Hyperlink_Click">
            <TextBlock Text="TestText" />
        </Hyperlink>
    </TextBlock>        

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Tag, ElementName=MyTextBlock}" Value="True">
            <Setter TargetName="MyHyperlink" Property="IsEnabled" Value="True" />
        </DataTrigger>

        <DataTrigger Binding="{Binding Path=Tag, ElementName=MyTextBlock}" Value="False">
            <Setter TargetName="MyHyperlink" Property="IsEnabled" Value="False" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

If Tag of TextBlock will be True , then Hyperlink is enabled, otherwise it will be disabled.

Also, you can Binding the property in DataTrigger . Add some property, for example HyperlinkIsEnabled to your Data, and in the DataTrigger write this:

<DataTrigger Binding="{Binding Path=HyperlinkIsEnabled}" Value="False">
    <Setter TargetName="MyHyperlink" Property="IsEnabled" Value="False" />
</DataTrigger>

To successfully updated your properties, your Data class must implement the INotifyPropertyChanged interface.

You can directly bind the IsEnabled property of the Hyperlink control to a property in your view model like rest of the properties.

<Hyperlink  NavigateUri="{Binding Data.Hyperlink}" 
Click="ClickonEmoHyperlink" TargetName="_blank">
    <TextBlock Text="{Binding Data.emo_number}" 
    IsEnabled="{Binding Data.IsEnabled}"/>
</Hyperlink>

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