【问题标题】:Binding data in C# and XAML?在 C# 和 XAML 中绑定数据?
【发布时间】:2012-08-25 22:53:28
【问题描述】:

我正在尝试在我的 Metro 应用程序中使用与 C# 和 XAML SemanticZoom 控件的绑定,但老实说,我不知道该怎么做。这是迄今为止我从问题、文章等中拼凑而成的 XAML 代码:

<SemanticZoom x:Name="boardZoom" Height="626" Margin="10,132,10,0" VerticalAlignment="Top">
        <SemanticZoom.ZoomedInView>

           <GridView IsSwipeEnabled="True" x:Name="ItemsGridView">

                    <GridView.ItemTemplate>

                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="10,10,0,0" 
                        HorizontalAlignment="Left" Background="White">
                                <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Width="200" Height="300"
                                    FontFamily="Global User Interface" FontSize="40" Foreground="Black"
                           VerticalAlignment="Center"  HorizontalAlignment="Left"/>

                                <Image Source="{Binding Image}" Height="60" Width="60" 
                       VerticalAlignment="Center" Margin="0,0,10,0" Visibility="{Binding isImage}" />

                                <TextBlock Text="{Binding Category}" TextWrapping="Wrap" Width="200"
                                    FontFamily="Global User Interface" Foreground="Black"
                           VerticalAlignment="Center"  HorizontalAlignment="Left"/>
                            </StackPanel>
                        </DataTemplate>

                    </GridView.ItemTemplate>

                </GridView>
            </SemanticZoom.ZoomedInView>

<!--Didn't include SemanticZoom.ZoomedOutView since I'm still trying to get the ZoomedIn one working first-->

        </SemanticZoom>

还有我的 C# 代码:

        List<PinStore.pin> pins = PinStore.CopyFromStream(response.GetResponseStream()); //returns a list of PinStore.pin objects, which have name, type, Image, isImage, and Category objects
        System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn> toSource = new System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn>(); //should I be using ObservableCollection or something like List<> here?
        foreach (PinStore.pin pin in pins)
        {
            SemanticZoomed.ZoomedIn toAdd = new SemanticZoomed.ZoomedIn(); //class with Title, Image, isImage, and Category objects
            if (pin.type == "text")
            {
                toAdd.Title = pin.name;
                toAdd.Image = null;
                toAdd.isImage = Visibility.Collapsed;
                toAdd.Category = pin.category;
            }
            toSource.Add(toAdd);
        }
        ItemsGridView.DataContext = toSource;

我在 XAML/C# 方面没有太多经验,并且在绑定方面零经验。我没有收到任何错误,但我注意到如果我用ItemsGridView.ItemsSource = toSource; 替换ItemsGridView.DataContext = toSource;,GridView 中会出现一个空白堆栈面板,我似乎找不到用标题和我指定的类别值。谢谢!

【问题讨论】:

    标签: c# xaml binding microsoft-metro


    【解决方案1】:

    您应该首先考虑创建一个 ResourceDictionary 以便保存您的项目样式。然后可以将 ItemStyle 设置为 Resource Dictionary。

    但不管你需要做什么:ItemsGridView.ItemsSource = toSource;如果不选择在 GridView xaml 中绑定 toSource。

    还要确保 SemanticZoomed.zoomedIn 对象实现 INotifyPropertyChanged 接口,并正确调用事件。并确保标题、图像、类别等是在编辑时调用事件的公共属性。并且您还需要确保 pin.Text 是实际值。 {确定}。

    如果您想了解有关数据绑定的更多信息,请查看他们如何在 Windows 8 中使用 C# 和 XAML 进行操作{应该是一样的}: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx

    【讨论】:

    • 感谢您的回复,我会在明天早上试用并回复您:) 有点离题,但我能问一下您为什么使用 {} 而不是 () 吗?只是好奇,如果您不想回答,请不要:D
    • 我使用任何看起来很酷的符号,包括 [] {} 和 ()。我猜是因为我程序太多,而且很容易按 { then (.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多