gpt4 book ai didi

c# - 在 prsim 模型中使用 ValueConverter

转载 作者:行者123 更新时间:2023-12-03 10:44:36 26 4
gpt4 key购买 nike

我有值转换器,它可以在控件的文本是否以 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>

我应该为可见性的绑定(bind)路径使用什么?我尝试通过在 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 超链接,但我的 datatagridtemplate 列的转换器似乎不起作用。但它适用于 datagridtextcolumn。有人可以指定如何使用我的转换器隐藏数据网格模板库。现在我的网格也显示为
 Column1  column2  Column3
name John pdf
CustomerID 1234 pdf
Customer_url hhtp://www.test.com pdf

我如何从其他行中删除 pdf 链接并仅将其保留用于客户 url 行。谢谢你

关于c# - 在 prsim 模型中使用 ValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32336725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com