create datatemplate code behind

前端 未结 3 480
一向
一向 2021-01-14 05:57

I am trying to create a ListBox view for show data, and I want it to contain a ListBox with a datatemplate for 2 columns \"Product ID & Product Barcode\"

I want

相关标签:
3条回答
  • 2021-01-14 06:42

    I have created DataTemplate like this:

    private DataTemplate getDataTemplate()
    {
        DataTemplate retVal = null;
        String markup = String.Empty;
    
        markup = "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns:local=\"clr-namespace:YOUR.PROJECT.NAMESPACE;assembly=YOUR.PROJECT.NAMESPACE\">";
        markup += "<Grid>";
        markup += "<TextBlock Text=\"{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content, Mode=OneWay}\" />";
        markup += "</Grid>";
        markup += "</DataTemplate>";
    
        retVal = (DataTemplate)XamlReader.Load(markup);
    
        return retVal;
    }
    

    ...and then call this method where you need it (like OnApplyTemplate)

    this.ContentTemplate = getDataTemplate();
    

    NOTE : You may have to change the "xmlns" for WPF, because this example is taken from one of my Silverlight projects. But the idea is the same.

    0 讨论(0)
  • 2021-01-14 06:52

    Conditional XAML DataTemplate

    Defining a static DataTemplate in your object's XAML file is the customary way to approach this. Also, the example Microsoft provides for DataTemplate.LoadContent() is nifty for showing how to switch templates dynamically at run-time (see DataTemplate.LoadContent method) when needed.

    However, if you have a special requirement of conditional XAML compilation (like omitting debug-only XAML when you're building a Release version), you will need to resort to the XamlReader.Load() approach (see XamlReader.Load method).

    To that end, I thought a bit more detailed example might be helpful. Here, I have a debug-only ListView that is bound to an ObservableCollection<> of custom objects. The ListView is not defined in static XAML, since it's needed only in Debug mode ...


    Custom class:

        class ActiveNotification
        {
            public String Name { get; set; }
            public String Type { get; set; }
            public String DayOfWeek { get; set; }
            public DateTime DeliveryTime { get; set; }
            public String Id { get; set; }
        }
    

    Private member variables:

        readonly ObservableCollection<ActiveNotification> _activeNotifications = new ObservableCollection<ActiveNotification>();
        readonly ListView listViewNotifications = 
            new ListView 
            { 
                Visibility = Visibility.Collapsed,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Bottom,
            };
    

    Load-time ListView setup:

            // Set up notifications list
            listViewNotifications.SetBinding(ListView.ItemsSourceProperty, new Binding { Source = _activeNotifications });
            listViewNotifications.Tapped += listViewNotifications_Tapped;
            Grid.SetRowSpan(listViewNotifications, 2);
            Grid.SetColumnSpan(listViewNotifications, 2);
            var xamlString =
                "<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" +
                    "<StackPanel Orientation=\"Horizontal\" VerticalAlignment=\"Center\">" +
                        "<TextBlock Text=\"{Binding Name}\" Margin=\"20,0,10,0\"/>" +
                        "<TextBlock Text=\"{Binding Type}\" Margin=\"0,0,10,0\"/>" +
                        "<TextBlock Text=\"{Binding DayOfWeek}\" Margin=\"0,0,10,0\"/>" +
                        "<TextBlock Text=\"{Binding DeliveryTime}\" Margin=\"0,0,10,0\"/>" +
                        "<TextBlock Text=\"{Binding Id}\"/>" +
                    "</StackPanel>" +
                "</DataTemplate>";
            var dataTemplate = (DataTemplate)XamlReader.Load(xamlString);
            listViewNotifications.ItemTemplate = dataTemplate;
            GridMain.Children.Add(listViewNotifications);
    
    0 讨论(0)
  • 2021-01-14 06:57

    You can define resources in xaml and retrieve it in code behind if it has an x:Key defined.

    In your xaml :

    <DataTemplate x:Key="anyId">...</DataTemplate>
    

    And in your code behind :

    var dataTemplate = Application.Current.TryFindResource("anyId") as DataTemplate;
    

    or

    var dataTemplate = Application.Current.FindResource("anyId") as DataTemplate;
    
    0 讨论(0)
提交回复
热议问题