ItemsControl 数据绑定...绑定到当前项目不起作用

发布于 2024-10-21 07:29:47 字数 2303 浏览 6 评论 0原文

我有以下 ItemsControl 页面...

<Page x:Class="Kiosk.View.ItemListView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Converter="clr-namespace:Kiosk.Converter" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Margin="10"
      Title="ItemListView">
    <StackPanel>
        <Button Width="130" Height="130" Style="{StaticResource DarkButton}" 
                Command="BrowseBack" Content="Back" HorizontalAlignment="Left" Margin="0,0,0,5"/>
        <ItemsControl ItemsSource="{Binding EventClassSummaries}">
            <ItemsControl.Resources>
                <Converter:OxiStringConverter x:Key="oxiStringConverter" />
            </ItemsControl.Resources>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Height="1000" Width="900" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=Name}"
                            Style="{StaticResource DarkButton}"
                            Height="42"
                            Width="440"
                            FontSize="12pt"
                            Margin="4"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Page>

EventClassSummaries 工作正常,因为我在包装面板中获得了正确数量的按钮,数据源自 WCF 服务,并且各个项目如下(来自服务端)

[DataContract]
public class EventClassSummary
{
    [DataMember] public string Category;
    [DataMember] public char Displayed;
    [DataMember] public int Id;
    [DataMember] public string Name;
    [DataMember] public char Status;
}

我遇到的问题是按钮不显示 Name 绑定,我只是得到空白。

奇怪的是,这在周五工作,但我不得不重建服务并添加一些额外的方法(尽管我没有触及与此相关的方法!?)

有人有想法吗,我觉得这有点奇怪工作(我什至向 PM 演示了它)

I have the following ItemsControl page...

<Page x:Class="Kiosk.View.ItemListView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:Converter="clr-namespace:Kiosk.Converter" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Margin="10"
      Title="ItemListView">
    <StackPanel>
        <Button Width="130" Height="130" Style="{StaticResource DarkButton}" 
                Command="BrowseBack" Content="Back" HorizontalAlignment="Left" Margin="0,0,0,5"/>
        <ItemsControl ItemsSource="{Binding EventClassSummaries}">
            <ItemsControl.Resources>
                <Converter:OxiStringConverter x:Key="oxiStringConverter" />
            </ItemsControl.Resources>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Height="1000" Width="900" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Path=Name}"
                            Style="{StaticResource DarkButton}"
                            Height="42"
                            Width="440"
                            FontSize="12pt"
                            Margin="4"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Page>

The EventClassSummaries is working correctly, as I am getting the right number of buttons in my wrap-panel, the data is sourced from a WCF service, and the individual items are as follows (from the service end)

[DataContract]
public class EventClassSummary
{
    [DataMember] public string Category;
    [DataMember] public char Displayed;
    [DataMember] public int Id;
    [DataMember] public string Name;
    [DataMember] public char Status;
}

The problem I have is the Buttons do not display the Name binding and I just get blanks.

Oddly this was working on Friday, but I've had to rebuild the service and add some additional methods (although I didn't touch the ones relevant to this!?)

Does anyone have ideas, I find it a bit strange that this used to work (I even demo'ed it to PM's)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2024-10-28 07:29:47

为了使 DataBinding 能够与 WPF 一起使用,您需要将字段更改为属性。

因此,将:更改

[DataMember] public string Name;

为:

public string _Name;
[DataMember]
public string Name
{
    get { return _Name; }
    set { _Name = value; }
}

旁注:希望您不介意我说教,但在表示层中引用数据传输类并不是一个好习惯 - 您应该考虑分离各层。

For DataBinding to work with WPF, you need to change your fields to properties.

So change:

[DataMember] public string Name;

to:

public string _Name;
[DataMember]
public string Name
{
    get { return _Name; }
    set { _Name = value; }
}

Side note: Hope you don't mind me preaching, but it is not good practice to be referencing your data transfer classes in the presentation layer - you should think about separating your layers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文