我將此檔案持有者標記為 2。在影像上。當我單擊Upload PDF時,我想在添加新檔案后從現有的 xaml 創建一個外觀完全相同的新檔案持有者。
我想問的是如何訪問 xaml 以便這成為可能?
XAML:
<UserControl
x:Class="ModernDesign.MVVM.View.LibraryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModernDesign.MVVM.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="460"
d:DesignWidth="800"
mc:Ignorable="d">
<StackPanel>
<TextBlock
Margin="0,20,0,0"
HorizontalAlignment="Center"
FontFamily="/Fonts/#TiroGurmukhi"
FontSize="28"
Foreground="White"
Text="Your PDF Library" />
<StackPanel
Margin="0,10,0,0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button
x:Name="UploadButton"
Width="70"
Background="White"
Click="UploadFileToLibrary"
Content="Upload PDF"
Cursor="Hand">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Border Width="192">
<StackPanel x:Name="FileContentHolder">
<Border
Width="110"
Height="105"
HorizontalAlignment="Center"
Background="#844eff"
CornerRadius="10">
<Button
x:Name="OpenPDF"
Width="45"
Margin="0,75,0,15"
HorizontalAlignment="Center"
Background="White"
Click="OpenFile">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
</Button.Resources>
<TextBlock
HorizontalAlignment="Center"
FontSize="9"
Foreground="#3b3939"
Text="Open" />
</Button>
</Border>
<TextBlock
x:Name="FileNameHolder"
Margin="0,5,0,0"
HorizontalAlignment="Center"
FontFamily="/Fonts/#TiroGurmukhi"
FontSize="12"
Foreground="White"
Text="FileName" />
</StackPanel>
</Border>
</StackPanel>
</StackPanel>
現有邏輯:
public partial class LibraryView : UserControl
{
string globalFilepath;
public LibraryView()
{
InitializeComponent();
}
public void UploadFileToLibrary(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
bool? response = openFileDialog.ShowDialog();
if (response == true)
{
string filepath = openFileDialog.FileName;
string filename = Path.GetFileNameWithoutExtension(filepath);
//string fullpath = Path.GetFullPath(filepath);
FileNameHolder.Text = filename;
globalFilepath = filepath;
}
}
public void OpenFile(object sender, RoutedEventArgs e)
{
var openSelectedFile = new Process();
openSelectedFile.StartInfo = new ProcessStartInfo(globalFilepath)
{ UseShellExecute = true };
openSelectedFile.Start();
}
}
uj5u.com熱心網友回復:
您應該將重復的布局元素定義為DataTemplate
,然后使用ListBox
. 的專案ListBox
是檔案名。
您可以重新定義ListBox.ItemsPanel
以將其配置為水平顯示專案。
主視窗.xaml
<Window>
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=UploadedFiles}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="192"
x:Name="Stack">
<StackPanel x:Name="FileContentHolder">
<Border Width="110"
Height="105"
HorizontalAlignment="Center"
Background="#844eff"
CornerRadius="10">
<Button x:Name="OpenPDF"
Width="45"
Margin="0,75,0,15"
HorizontalAlignment="Center"
Background="White"
Click="OpenFile">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius"
Value="5" />
</Style>
</Button.Resources>
<TextBlock HorizontalAlignment="Center"
FontSize="9"
Foreground="#3b3939"
Text="Open" />
</Button>
</Border>
<TextBlock x:Name="FileNameHolder"
Margin="0,5,0,0"
HorizontalAlignment="Center"
FontFamily="/Fonts/#TiroGurmukhi"
FontSize="12"
Foreground="White"
Text="{Binding}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
主視窗.xaml.cs
// The binding source for the ListView
public ObservableCollection<string> UploadedFiles
{
get => (ObservableCollection<string>)GetValue(UploadedFilesProperty);
set => SetValue(UploadedFilesProperty, value);
}
public static readonly DependencyProperty UploadedFilesProperty = DependencyProperty.Register(
"UploadedFiles",
typeof(ObservableCollection<string>),
typeof(MainWindow),
new PropertyMetadata(default));
public MainWindow()
{
InitializeComponent();
this.UploadedFiles = new ObservableCollection<string>();
// TODO::If necessary initialize UploadedFiles collection with filenames
}
public void UploadFileToLibrary(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
bool? response = openFileDialog.ShowDialog();
if (response == true)
{
string filepath = openFileDialog.FileName;
string filename = Path.GetFileNameWithoutExtension(filepath);
// Display a new "fileholder" box in the view
this.UploadedFiles.Add(filename);
...
}
...
}
看
- Microsoft Docs:資料模板概述
- Microsoft Docs:依賴屬性概述
- Microsoft Docs:資料系結概述 (WPF .NET)
uj5u.com熱心網友回復:
StackPanel
您可以使用System.Windows.Markup.XamlWriter
該類克隆:
string xaml = XamlWriter.Save(FileContentHolder);
StackPanel newStackPanel = (StackPanel)XamlReader.Parse(xaml);
Button openPdf = newStackPanel.FindName("OpenPDF") as Button;
if (openPdf != null)
{
openPdf.Click = OpenFile;
}
root.Children.Add(newStackPanel);
請注意,您必須以編程方式重新附加任何事件處理程式:
XamlWriter.Save 的序列化限制
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494723.html
下一篇:導航到新頁面時的WPF依賴項注入