我有一个数据网格,我希望能够检测我的列是否使用XAML显示0,在这种情况下用“ - ”替换零。
这是我的datagrid:
<DataGrid Style="{DynamicResource StyleDataGrid}" ItemsSource="{Binding Path=_Data, UpdateSourceTrigger=PropertyChanged}" Margin="10,54,10,10" Name="dataGridView1" >
<DataGrid.Columns>
<DataGridTextColumn Width="35" Binding="{Binding Path=Object.MyValue}" SortMemberPath="Object.MyValue" Header="" IsReadOnly="true">
<DataGridTextColumn.CellStyle>
<Style BasedOn="{StaticResource {x:Type DataGridCell}}" TargetType="DataGridCell">
<Setter Property="Foreground" Value="{Binding BrushTroisPts}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=TextBlock.Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock}}}" Value="0">
<Setter Property="TextBlock.Text" Value="-"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
但这不起作用,我怎样才能使它发挥作用?
谢谢
答案 0 :(得分:1)
您可以使用转换器来执行此操作。查看DataGridTextColumn上的绑定,MyConverter的代码以及转换器注册的window.resources。我把一些课程弄得与你已经拥有的课程相匹配。
XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="DataGrid" x:Key="StyleDataGrid" />
<app:MyConverter x:Key="MyConverter" />
</Window.Resources>
<Grid>
<DataGrid Style="{DynamicResource StyleDataGrid}" ItemsSource="{Binding Path=_Data, UpdateSourceTrigger=PropertyChanged}" Margin="10,54,10,10" Name="dataGridView1" >
<DataGrid.Columns>
<DataGridTextColumn Width="35" Binding="{Binding Path=Object.MyValue, Converter={StaticResource MyConverter}}" SortMemberPath="Object.MyValue" Header="" IsReadOnly="true">
<DataGridTextColumn.CellStyle>
<Style BasedOn="{StaticResource {x:Type DataGridCell}}" TargetType="DataGridCell">
<Setter Property="Foreground" Value="{Binding BrushTroisPts}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
代码(C#):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public List<MyData> _Data { get; set; }
public MainWindow()
{
InitializeComponent();
_Data = new List<MyData>();
for (int i = 0; i < 10; i++)
{
_Data.Add(new MyData(i.ToString()));
}
this.DataContext = this;
}
}
//Object.MyValue
public class MyData
{
public MyObject Object { get; set; }
public MyData(string val)
{
Object = new MyObject(val);
}
}
public class MyObject
{
public string MyValue { get; set; }
public MyObject(string val)
{
MyValue = val;
}
}
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ("0".Equals(value))
return "-";
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}