请帮助使用此语法 [英] Please help with this syntax
问题描述
我有一个组合框,其列表项在后面的代码中填充.但是,我正在使用DataTemplate并在其中包含此TextBlock. ****怎么了?基本上,下拉列表中的值需要传递给转换器.
I have a combo-box whose list items are populated in code behind. However, I am using a DataTemplate and have this TextBlock in it. What goes at ****? Basically, the values in the drop-down need to be passed to the converter.
< TextBlock Text =" {Binding}"前景="{绑定****,转换器= {StaticResource colorConverter}}" />
<TextBlock Text="{Binding}" Foreground="{Binding ****, Converter={StaticResource colorConverter}}" />
谢谢.
推荐答案
根据您的描述,我不完全了解您想要实现的目标,您能否更详细地描述您的问题?你想要什么?
According to your description, I don't fully understand what you want to achieve, can you describe your problem in more detail ? what do you want?
下面是我的示例,以演示如何使用转换器.希望这对您有所帮助.
Below is my sample to show how to use the Converter. Hope this is helpful to you.
在XMAL中:
<Window.Resources>
<local:ColorConverter x:Key="colorConverter" />
</Window.Resources>
<StackPanel
<TextBlock
Width="80"
Height="25"
Margin="0,7,0,7"
Background="{Binding ElementName=ComboBox_ColorSelect, Path=SelectedValue, Converter={StaticResource colorConverter}}" />
<ComboBox
x:Name="ComboBox_ColorSelect"
Width="80"
Height="25"
Cursor="Hand"
DisplayMemberPath="Name"
ItemsSource="{Binding ColorList}"
SelectedValue="{Binding SelectedColor}"
SelectedValuePath="ID" />
</StackPanel>
C#:
public partial class ConverterWindow : Window
{
public ConverterWindow ()
{
InitializeComponent();
this.DataContext = new ConverterViewModel();
}
}
public class ConverterViewModel
{
public class ColorInfo
{
public int ID { get; set; }
public string Name { get; set; }
}
public IList<ColorInfo> ColorList { get; set; }
public int SelectedColor { get; set; }
public ConverterViewModel()
{
ColorList = new List<ColorInfo>()
{
new ColorInfo(){ ID=0, Name="red"},
new ColorInfo(){ ID=1, Name="green"},
new ColorInfo(){ ID=2, Name="blue"}
};
SelectedColor = 1;
}
}
public class ColorConverter : IValueConverter
{
public static Color[] ColorCollection = new Color[] {
Color.FromRgb(255,0,0),//red
Color.FromRgb(0,255,0),//green
Color.FromRgb(0,0,255)//blue
};
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return new SolidColorBrush(ColorCollection[0]);
try
{
return new SolidColorBrush(ColorCollection[(int)value]);
}
catch
{
return new SolidColorBrush(ColorCollection[0]);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最好的问候,
Best Regards,
鲍勃
这篇关于请帮助使用此语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!