【发布时间】:2015-01-30 17:51:57
【问题描述】:
我正在尝试显示一个 Items 控件,其中包含另一个表示 LED 指示灯数组的 Items 控件。 LED 阵列和主 Item Control 中的所有其他数据都绑定到一个 Observable 集合。我无法让 LED 阵列显示。我也知道我在使用 IValueConverter 将字节转换为画笔颜色时遇到问题(整个字节数组都进来了,但我希望它一次只做一个元素,即使我硬编码 LED 的返回值也不会' t 显示)。我只是不知道我做错了什么(我对这一切都很陌生)。提前感谢您的任何建议!
我的 ValueConverter 代码
namespace Test
{
public class ByteToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((byte)value == 1) ? Brushes.LightGreen : Brushes.DarkOliveGreen;
}
}
class userData : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private byte[] led;
public byte[] Led
{
get { return led; }
set
{
byte[] input = value;
if (input != this.led)
{
this.led = input;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我的 Xaml 代码
<Window x:Class="Test.MainWindow"
xmlns:src="clr-namespace:Test"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" WindowState="Maximized" WindowStyle="None">
<Window.Resources>
<src:ByteToBrushConverter x:Key="LEDConverter" />
<DataTemplate x:Key="myLedTemplate" DataType="{x:Type sys:Byte}">
<Grid>
<Border Height="12" Width="12" BorderThickness="1" BorderBrush="Black" Background="{Binding Led,Converter={StaticResource LEDConverter}}" />
</Grid>
</DataTemplate>
<DataTemplate x:Key="myHwTemplate">
<Grid Margin="10,10,10,10" MinWidth="110" MinHeight="90">
<Border BorderBrush="Black" BorderThickness="2" >
<Grid Margin="0,0,0,0" >
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" x:Name="userControlNameLabel" Content="{Binding Path=name}" />
<Label Grid.Row="1" x:Name="powerLabel" Background="{Binding Path=Power, Converter={StaticResource int2color}}" HorizontalAlignment="Stretch" Content="Powered" HorizontalContentAlignment="Center" Margin="5,1,5,1" />
<Grid Grid.Row="2" Margin="5,5,0,0">
<ItemsControl ItemsSource="{Binding Path=Led,Converter={StaticResource LEDConverter}}" ItemTemplate="{StaticResource myLedTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Grid>
</Border>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid Margin="0,60,0,0" >
<ItemsControl Name="ssedu0ItemControl" ItemTemplate="{StaticResource myHwTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Grid>
</Window>
这是我的主要代码
namespace Test
{
public partial class MainWindow : Window
{
private ObservableCollection<userData> ssedu0LBData = new ObservableCollection<userData>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < Properties.Settings.Default.unit.Count; i++)
ssedu0LBData.Add(new userData(Properties.Settings.Default.unit[i]));
ssedu0ItemControl.ItemsSource = ssedu0LBData;
for(int i=0;i<8;i++)
ssedu0LBData[1].Led[i] = 1;
}
}
}
【问题讨论】:
-
顺便说一句,这个问题做得很好,它有很多代码,但您包含 刚好 来显示所有问题。赞一个!
标签: c# arrays wpf datatemplate itemscontrol