在 prsim 模型中使用 ValueConverter
Use a ValueConverter in prsim model
我有一个值转换器,可以在文本是否以 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.Windows.Data.Binding' 的对象无法转换为类型 'System.Windows.Visibility'。
我终于能够查明错误背后的原因:
我已经为 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 hyperlink,但是我的 datatagridtemplate 列转换器似乎不起作用。虽然它适用于 datagridtextcolumn。有人可以指定如何使用我的转换器隐藏 datagridtemplatecoulumn 吗?现在我的网格显示为
Column1 column2 Column3
name John pdf
CustomerID 1234 pdf
Customer_url hhtp://www.test.com pdf
我如何从其他行中删除 pdf link 并只为客户 url 行保留它。谢谢
我有一个值转换器,可以在文本是否以 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.Windows.Data.Binding' 的对象无法转换为类型 'System.Windows.Visibility'。
我终于能够查明错误背后的原因:
我已经为 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 hyperlink,但是我的 datatagridtemplate 列转换器似乎不起作用。虽然它适用于 datagridtextcolumn。有人可以指定如何使用我的转换器隐藏 datagridtemplatecoulumn 吗?现在我的网格显示为
Column1 column2 Column3
name John pdf
CustomerID 1234 pdf
Customer_url hhtp://www.test.com pdf
我如何从其他行中删除 pdf link 并只为客户 url 行保留它。谢谢