我正在尝试显示一个Items Control,其中包含另一个表示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;
}
}
}
答案 0 :(得分:0)
你的主要问题在于:
ItemsSource="{Binding Path=Led,Converter={StaticResource LEDConverter}}"
您不想通过转换器运行集合,当然您的转换器目前还不能处理集合。那条线应该是:
ItemsSource="{Binding Path=Led}"
稍后您将在ItemControl的数据模板中使用转换器,当您执行以下操作时:
<Border BorderBrush="{Binding Path=. Converter={StaticResource LEDConverter}}"/>
关于您的代码的警告。更改该阵列的各个元素将不传播到UI。您需要在实现INPC的byte
周围使用包装器,或者只替换整个数组而不是更改单个元素。