我有一个值转换器,当文本以http开头或不以http开头时,它会转换控件的可见性方面。以下是代码。
public class HTTPVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool boolVisbility = (value != null) && value.ToString().StartsWith("http");
boolVisbility = (parameter != null) ? !boolVisbility : boolVisbility;
return boolVisbility ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我已将此代码放在ToolViewe.xaml.cs中。 我试图在我的数据网格中使用上面的转换器,如下所示:
<slData:DataGrid x:Name="CustomerDetailsDataGrid"
ItemsSource=" {Binding SingleReturnedItemAttributes}"
AutoGenerateColumns="False"HeadersVisibility="None"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="350"
MaxWidth="350">
<slData:DataGrid.Columns>
<slData:DataGridTextColumn Binding=" {Binding Path=Key}"
FontWeight="Bold"/>
<slData:DataGridTextColumn Binding="{Binding Path= Value}" Visibility="
{Binding Path=IsControlVisible,Converter={StaticResource theHttpVisbilityConverter}}"
/>
<slData:DataGridTextColumn Binding="{Binding Path= Value}" Visibility="
{Binding Path=IsControlVisible ,Converter={StaticResource theHttpVisbilityConverter},
ConverterParameter=reverse}"
/>
</slData:DataGrid.Columns>
我应该将什么用于可见性的绑定路径?我已经尝试通过声明ToolViewModel.cs中的代码来使用以下属性。但是不起作用。请指导这个新手。
bool isControlVisible = false;
public bool IsControlVisible
{
get { return isControlVisible; }
set
{
isControlVisible = value;
this.RaisePropertyChanged(() => this.IsControlVisible);
}
}
这是错误:类型为#System.SWindows.Data.Binding&#39;的对象无法转换为类型&#39; System.Windows.Visibility&#39;。
答案 0 :(得分:0)
最后我能够解决我的错误背后的原因:
我已经为xaml.cs中的datagridtextcolumn和datagridtemplatecolumn的可见性创建了依赖项。
public class VisibilityData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _visible = true;
public bool Visible
{
get { return this._visible; }
set
{
if (this._visible != value)
{
this._visible = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
}
}
}
}
}
public class ExtendedDataGridTextColumn : DataGridTextColumn
{
public Visibility MyVisibility
{
get
{
return (Visibility)GetValue(MyVisibilityProperty);
}
set
{
SetValue(MyVisibilityProperty, value);
}
}
public static readonly DependencyProperty MyVisibilityProperty = DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(ExtendedDataGridTextColumn), new PropertyMetadata(MyVisibilityChanged));
private static void MyVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var n = d as ExtendedDataGridTextColumn;
if (n != null && e.NewValue is Visibility)
{
n.Visibility = (Visibility)e.NewValue;
}
}
public ExtendedDataGridTextColumn()
{
}
}
public class CustomDataGridTemplateColumn : DataGridTemplateColumn
{
public static readonly DependencyProperty VisibilityBindingProperty = DependencyProperty.Register(
"VisibilityBinding", typeof(Visibility), typeof(CustomDataGridTemplateColumn), new PropertyMetadata(Visibility.Collapsed, new PropertyChangedCallback(OnVisibilityChanged)));
public Visibility VisibilityBinding
{
get { return (Visibility)this.GetValue(VisibilityBindingProperty); }
set { this.SetValue(VisibilityBindingProperty, value); }
}
private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((CustomDataGridTemplateColumn)d).Visibility = (Visibility)e.NewValue;
}
}
现在这就是我的数据网格的样子:
<slData:DataGrid x:Name="Customer" ItemsSource="{Binding ReturnedItemAttributes}" AutoGenerateColumns="False" HeadersVisibility="None" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="350" MaxWidth="350">
<slData:DataGrid.Resources>
<ResourceDictionary>
<local:HTTPVisibilityConverter x:Key="httpVisibility"/>
<local:VisibilityData x:Key="visibilityValue"/>
</ResourceDictionary>
</slData:DataGrid.Resources>
<slData:DataGrid.Columns>
<local:ExtendedDataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>
<local:ExtendedDataGridTextColumn Binding="{Binding Path=Value}" MyVisibility="{Binding Visible, Source={StaticResource visibilityValue}, Converter={StaticResource httpVisibility}, ConverterParameter=Reverse}"/>
<local:CustomDataGridTemplateColumn Width="40" VisibilityBinding="{Binding Visible, Source={StaticResource visibilityValue}, Converter={StaticResource httpVisibility}}">
<local:CustomDataGridTemplateColumn.CellTemplate>
<DataTemplate>
<HyperlinkButton Content="Pdf Link" Height="23" Name="PdfUrl" Width="76" NavigateUri="{Binding Path=Value, Mode=TwoWay}" TargetName="_blank" Margin="5,10,10,7" Foreground="Blue" />
</DataTemplate>
</local:CustomDataGridTemplateColumn.CellTemplate>
</local:CustomDataGridTemplateColumn>
</slData:DataGrid.Columns>
</slData:DataGrid>
这就是我解决之前错误的方法。
所以现在我可以在单独的列中看到pdf超链接,但我的datatagridtemplate列的转换器似乎不起作用。它适用于datagridtextcolumn。有人可以指定如何使用我的转换器隐藏datagridtemplatecoulumn。现在我的网格显示为
Column1 column2 Column3
name John pdf
CustomerID 1234 pdf
Customer_url hhtp://www.test.com pdf
如何从其他行中删除pdf链接,并仅将其保留在Customer url行中。谢谢