简体   繁体   中英

C# wpf - Adding to xaml file during execution

This is what my main program GUI will look like, what I am attempting to do, is to create a reminder application in C# using wpf.

I am going to use a scroll viewer which is going to be displaying the data to the user, namely the reminders they currently have.

Each time the user adds a reminder, it will add this:

What I am wanting to do is that, when ever the user add's a new reminder, there will be a new set of data added to the scrollviewer.

What would be the best way of achieving this?

Am I able to hold the xaml data and add it during execution?

Thanks for the help

What you want to do can be accomplished not by dynamic Xaml, but by the use of a templated control which can accept dynamic data. For example you wouldn't consider using a listbox for your labels because you are not showing the data in a list right?

But a listbox is just a conveyor belt for what you want to achive. Say you want more than a label, how about three labels. Via binding to a proper structure you can get what is needed.

Here is an example

<ListBox ItemsSource="{Binding myReminders }">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=ReminderName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

Once that is bound to a list of my reminder data objects (which the list can dynamically change), we have the ability to show the reminders in any format we want. One just uses the above to style it appropriately. (Check out WPF's Templating Overview for a great example).

The use of templates is done in other controls, so if the listbox is not to your liking, look into other templated controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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