简体   繁体   中英

How to Bind dynamic values in Xaml using WPF in c#

I have created application in WPF with two window. In one window i have used with one text box and submit button. Once submit from first window i hide first window and show second window. I have taken some values using first window text value and need to bind in second window Xaml. Actually that values can bind using foreach in html(mvc) but need to bind Xaml for display in second window. Please give some suggestions.

Please find the below answer, It will work definitely

Xaml :

<ItemsControl Name="icTodoList">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="200,50,0,30">
                                    <TextBlock>
                                       <Hyperlink TextDecorations="None" NavigateUri="{Binding UriPath}" RequestNavigate="Hyperlink_RequestNavigate"
                                                  CommandParameter="{Binding ElementName=myImg}">
                                                        <Image HorizontalAlignment="Left" Width="80" Height="80"  x:Name="myImg" Source="{Binding Source}" Margin="5"/>
                                       </Hyperlink>
                                    </TextBlock>

                                    <TextBlock TextAlignment="Left" Margin="200,30,0,0">
                                            <TextBlock FontSize="22px" Text="{Binding Title}" Foreground="white"></TextBlock>
                                    </TextBlock>
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

C# code for binding Values,

 public class DMScreen3 {
    public List<string> AllFiles { get; set; }
              List<BindingFilesContent> items = new List<BindingFilesContent>();
                    if(AllFiles != null)
                    {
                        foreach(var r in AllFiles)
                        {
                            if ((r.ToLower().Contains(".avi") || r.ToLower().Contains(".mp4")) && fileTypes == "video")
                            {
                                items.Add(new BindingFilesContent() { Title = Path.GetFileName(r), UriPath = r, Source = "/images/videoicon.png" });
                            }
        icTodoList.ItemsSource = items;
                  }
        }
}
  public class BindingFilesContent
    {
        public string Title { get; set; }
        public string Source { get; set; }
        public string UriPath { get; set; }
    }

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