我不知道你是如何嘗試做this.Any方式我都試過,它正在fine.Check這個代碼,並找出什麼錯誤
的XAML:
<StackPanel Loaded="StackPanel_Loaded" >
<StackPanel.Resources>
<WpfApplication50:ValueToForegroundColorConverter x:Key="valueToForegroundColorConverter"/>
<DataTemplate x:Key="Valuetemplate">
<TextBlock x:Name="txt" Text="{Binding Value}" Foreground="{Binding Path=Value,Converter={StaticResource valueToForegroundColorConverter}}"/>
</DataTemplate>
</StackPanel.Resources>
<dtgrd:DataGrid ItemsSource="{Binding Source}"
Name="datagrid"
ColumnHeaderHeight="25"
AutoGenerateColumns="False"
>
<dtgrd:DataGrid.Columns>
<dtgrd:DataGridTemplateColumn CellTemplate="{StaticResource Valuetemplate}" Header="Value"/>
</dtgrd:DataGrid.Columns>
</dtgrd:DataGrid>
<Button Height="30" Click="Button_Click"/>
</StackPanel>
並在您的代碼隱藏
public partial class Window10 : Window,INotifyPropertyChanged
{
private DataTable source;
public DataTable Source
{
get { return source; }
set { source = value; OnPropertyChanged("Source"); }
}
public Window10()
{
InitializeComponent();
this.DataContext = this;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if(PropertyChanged!=null)
PropertyChanged(this,new PropertyChangedEventArgs(name));
}
#endregion
private void Button_Click(object sender, RoutedEventArgs e)
{
Source.Rows.Add("-1");
}
private void StackPanel_Loaded(object sender, RoutedEventArgs e)
{
Source = new DataTable();
Source.Columns.Add("Value");
Source.Rows.Add("1");
}
}
也該轉換器
class ValueToForegroundColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
SolidColorBrush brush = new SolidColorBrush(Colors.Black);
int val = 0;
int.TryParse(value.ToString(), out val);
if (val < 0)
brush = new SolidColorBrush(Colors.Red);
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
如果您提供一些重現問題的示例代碼,我們可以更輕鬆地幫您解答問題 – 2010-12-09 22:26:48