在prsim模型中使用ValueConverter

本文关键字:ValueConverter prsim 模型 | 更新日期: 2023-09-27 18:10:54

我有一个值转换器,当控件的文本是否以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();
    }
}

我已经把这段代码在toolview .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类型的对象。绑定'不能转换为'System.Windows.Visibility'类型

在prsim模型中使用ValueConverter

我终于找到了错误背后的原因:

我已经在xml .cs中创建了datagridtextcolumn和datagridtemplatecolcolumn的可见性依赖。

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链接,并保持它仅为客户url行。谢谢你