당신은
DataGridCell
의
Content
속성에 바인딩하고
Text
속성이 설정되고 나면
TextBlock
의
Foreground
속성을 설정할 수하기 위해
DependencyPropertyDescriptor
을 사용할 수
:
<DataGridTextColumn Binding="{Binding Path=., Converter={StaticResource GooglePositionConvertor}}">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding Path=Content, RelativeSource={RelativeSource Self},
Converter={StaticResource ChangeBrushColour}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
TextBlock content = value as TextBlock;
if(content != null)
{
string text = content.Text;
if(string.IsNullOrEmpty(text))
{
DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock));
if (dpd != null)
dpd.AddValueChanged(content, OnTextChanged);
}
else
{
OnTextChanged(content, EventArgs.Empty);
}
}
return Binding.DoNothing;
}
private void OnTextChanged(object sender, EventArgs e)
{
TextBlock textBlock = sender as TextBlock;
string converterText = textBlock.Text;
//set foreground based on text...
textBlock.Foreground = Brushes.Violet;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
출처
2017-05-23 09:45:29
mm8
첫 번째 변환기 인 "GooglePositionConvertor"의 행에 바인딩 된 개체가 필요하지만 th의 셀 값만 필요합니다. e 두 번째 변환기 "ChangeBrushColour". – jamie