Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top