【问题标题】:Stuck adding array of tooltips to data template of an array of LEDs将工具提示数组添加到 LED 数组的数据模板时卡住了
【发布时间】:2015-02-11 13:12:42
【问题描述】:

我在这个问题上被困太久了。我已经尝试了很多选项,但我无法让语法正确。我尝试过的所有选项都显示数据类型、所有元素的相同名称、空白工具提示、无工具提示或 LED 位值。我可以显示一个 LED 数组,其中颜色由位值确定,但我需要将每个元素的名称添加到工具提示中。我使用数据模板来显示数组。

我最近的 xaml:

    <Window.Resources>           
        <DataTemplate x:Key="myLedTemplate" DataType="{x:Type sys:Byte}" >
                <Border ... Background="{Binding Converter={StaticResource LEDConverter}}" ToolTip="{Binding Ledname}"/>              
        </DataTemplate>
        <DataTemplate x:Key="myHwTemplate">            
            <Grid>
               <Border>
                    <Grid>
                        <ItemsControl ItemsSource="{Binding Path=Led}" ItemTemplate="{StaticResource myLedTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
                            <ItemsControl.ItemsPanel>
                                ...
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ToolTip>
                                <ToolTip>
                                    <TextBlock Text="{Binding Path=Ledname}"/>
                                </ToolTip>
                            </ItemsControl.ToolTip>
                        </ItemsControl>                                 
                    </Grid>
                </Border>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ItemsControl Name="hwItemControl" ItemTemplate="{StaticResource myHwTemplate}" ... >
                ...
        </ItemsControl>
    </Grid>
</Window>

我的班级类型

private byte[] led;
public byte[] Led
{
    get { return led; }
    set
    {
        byte[] input = value;
        if (input != this.led)
        {
            this.led = input;
            NotifyPropertyChanged();
        }
    }
}

private string[] ledname;
public string[] Ledname
{
    get { return ledname; }
    set
    {
        string[] input = value;
        if (input != this.ledname)
        {
            this.ledname = input;
            NotifyPropertyChanged();
        }
    }
} 

就目前而言,如果我将鼠标悬停在 LED 元素上,它会显示“string[] Array”。感谢您提供的任何建议。

【问题讨论】:

  • 因为 Ledname 是 string[]。你想展示什么?您想在工具提示中查看整个列表,还是每个 LED 都有一个对应的 LED 名称?
  • 第二个选项。每个 LED 一个名称。

标签: arrays wpf data-binding tooltip datatemplate


【解决方案1】:

您正在绑定到一个字节数组,并且在您的项目模板中,您试图获取 LED 名称,但它位于不同的数组中。您的项目模板将仅引用 ItemsSource 属性的对象类型。

试试这个:

public class LED : INotifyPropertyChanged
{
    private int _Value;

    public int Value
    {
        get { return _Value; }
        set { _Value = value; NotifyPropertyChanged(); }
    }

    private string _Name;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged([CallerMemberName]string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

创建一个名为 LED 的对象,它将保存您的 LED 值和 LED 名称,然后您可以使用此类的 ObservableCollection 并绑定到它。

public ObservableCollection<LED> LEDs { get; set; }

将您的 ItemsSource 绑定到集合 LED,并绑定到项目模板内 LED 类中的属性。

<ItemsControl ItemsSource="{Binding Path=LEDs}" ...

<ItemsControl.ToolTip>
   <ToolTip>
     <TextBlock Text="{Binding Path=Name}"/>
   </ToolTip>
</ItemsControl.ToolTip>

【讨论】:

  • 我正在努力实现这一点,但这并不是那么简单。我的新 LED ObservableCollection 是更大的 ObservableCollection 的一个元素,我们对这些更改的尝试导致 LED 阵列停止显示。一旦再次起作用,工具提示应该就位了。
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
相关资源
最近更新 更多