C# 数据绑定和默认值
我拥有的是一个DataGrid(在WPF中),我将它绑定到我创建的自定义类的列表中 为了简单起见,我们假设该类如下所示:C# 数据绑定和默认值,c#,wpf,data-binding,datagrid,C#,Wpf,Data Binding,Datagrid,我拥有的是一个DataGrid(在WPF中),我将它绑定到我创建的自定义类的列表中 为了简单起见,我们假设该类如下所示: public class MyClass{ int val; string str; static const int alwaysSetValue = 10; } 有没有办法(在数据绑定或类本身中)说“如果val=-1,在datagrid中,而不是显示-1,只显示一个空格,或者“” 我正在查看绑定的IsTargetNull值,如果int是一个可为nu
public class MyClass{
int val;
string str;
static const int alwaysSetValue = 10;
}
有没有办法(在数据绑定或类本身中)说“如果val=-1,在datagrid中,而不是显示-1,只显示一个空格,或者“”
我正在查看绑定的IsTargetNull值,如果int是一个可为null的类型,这将很好,但如果可能的话,我宁愿不使用int
有没有办法做到这一点?某种重写ToString()之类的
解决方案
请参阅下面的答案。我所做的唯一更改是在代码中设置绑定和转换:
DataGrid.Columns.Add(new DataGridTextColumn() { Header = "Value", Binding = new Binding("val") { Converter = new MyValConverter() } });
下面是一个例子:
XAML文件:
<Window x:Class="DataGridConverter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:local="clr-namespace:DataGridConverter"
>
<Window.Resources>
<local:MyValConverter x:Key="myCon" />
</Window.Resources>
<Grid>
<DataGrid Name="grid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Val" Binding="{Binding val, Converter={StaticResource myCon}}" />
<DataGridTextColumn Header="Str" Binding="{Binding str}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
代码隐藏文件:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
namespace DataGridConverter
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<MyClass> _source = new List<MyClass>();
for (int i = 0; i < 5; i++)
{
_source.Add(new MyClass { val = 1, str = "test " + i });
}
_source[2].val = -1;
grid.ItemsSource = _source;
}
}
public class MyClass
{
public int val { get; set; }
public string str { get; set; }
const int alwaysSetValue = 10;
}
public class MyValConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value == -1 ? string.Empty : value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Data;
使用System.Windows.Documents;
命名空间DataGridConverter
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表_source=新列表();
对于(int i=0;i<5;i++)
{
_Add(新的MyClass{val=1,str=“test”+i});
}
_来源[2]。val=-1;
grid.ItemsSource=\u源;
}
}
公共类MyClass
{
公共int val{get;set;}
公共字符串str{get;set;}
const int alwaysSetValue=10;
}
公共类MyValConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回(int)值==-1?字符串。空:值;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
}
我将对此进行研究!谢谢:)完美,正是我所需要的,只是我根据代码设置了转换器(如果其他人想知道如何设置,请参见上面的示例)