Within a datagrid I would like to put an hyperlink in the content of the cell
<sdk:DataGridTextColumn Binding="{Binding Code}" Header="Code" Width="40" HeaderStyle="{StaticResource myStyle}" />
only if the content of the MyUrl property [of the sam开发者_开发百科e object that contains the Code I'm already binding against] is neither null or empty
How do I do this?
Assuming you want this Column to be readonly you could use a DataGridTemplateColumn, put in a HyperLinkButton and bind IsHitTestVisible to MyUrl with a converter like this
<sdk:DataGridTemplateColumn Header="Code" Width="40">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton IsHitTestVisible="{Binding Path=MyUrl,
Converter={StaticResource InvertNullOrEmptyConverter}}"
Content="{Binding Code}" NavigateUri="{Binding MyUrl}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
And in the Converter
public class InvertNullOrEmptyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string valueToCheck = value as string;
if (valueToCheck == null || valueToCheck == string.Empty)
{
return false;
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
精彩评论