繁体   English   中英

另一个项目控件/数据模板中的项目控件和项目模板

[英]Items Control and Items Template within another Items Control/Data Template

我正在尝试显示一个Items控件,其中包含另一个表示LED指示器阵列的Items控件。 LED阵列和主项目控件中的所有其他数据都绑定到一个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;
        }   
    }
}

您的主要问题在这里:

ItemsSource="{Binding Path=Led,Converter={StaticResource LEDConverter}}" 

您不想通过转换器运行集合 ,并且您的转换器当前不能在集合上运行。 因此,该行应为:

ItemsSource="{Binding Path=Led}" 

稍后,您可以在该ItemsControl的数据模板中使用该转换器,例如:

<Border BorderBrush="{Binding Path=. Converter={StaticResource LEDConverter}}"/>

关于您的代码的警告。 更改该数组的各个元素将不会传播到UI。 您需要在实现INPC的byte周围使用包装器,或者仅替换整个数组而不更改单个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM