![](/img/trans.png)
[英]How to provide the ID to the Textbox that is inside the Datagrid's datatemplate in wpf in mvvm model, So that I can differentiate the textbox
[英]How can I create this kind of model on Visual Studio WPF?[TreeList to TextBox]
哦,大家好,我是 C# 世界的新手,但我真的很想知道是否可以這樣做以及如何做。 基本上是一個程序模型,帶有一個 Datagridview,其中每個單元格都包含一個 Treelist,它指向一個可選文本框。 我試圖在互聯網上到處找到這個特定的模型,但沒有成功。
模型: ProgramModel.exe
對不起,如果漫畫廢話惹惱了你。
您好,我認為在這種情況下使用 ListView 控件可能會更好,您可以使用 DataTemplate 來設置 ListView 的樣式並告訴它如何顯示項目,首先添加這些 XAML 代碼:
<ListView x:Name="MainListView" HorizontalAlignment="Center" VerticalAlignment="Center">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Border BorderBrush="Black" BorderThickness="3,3,3,3" Margin="1,0,0,0" MinWidth="200">
<TextBlock Text="{Binding Path=TxtType}" Margin="10,0,0,0"></TextBlock>
</Border>
<TreeView>
<Border BorderBrush="Black" BorderThickness="3,3,3,3" Margin="-20,0,0,0" MinWidth="200">
<TreeViewItem Header="smalpptext">
<TreeViewItem.Items>
<TextBox Text="{Binding Path=SmalPPTxt}" IsReadOnly="True"></TextBox>
</TreeViewItem.Items>
</TreeViewItem>
</Border>
<Border BorderBrush="Black" BorderThickness="3,3,3,3" Margin="-20,0,0,0" MinWidth="200">
<TreeViewItem Header="largepptext">
<TreeViewItem.Items>
<TextBox Text="{Binding Path=LargePPTxt}" IsReadOnly="True"></TextBox>
</TreeViewItem.Items>
</TreeViewItem>
</Border>
</TreeView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在我們准備好 ListView 之后,我們需要用數據填充它,我們可以使用 ObservableCollection,添加這些 C# 代碼:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
AddText();
MainListView.ItemsSource = StringList;
}
ObservableCollection<PPStrings> StringList = new ObservableCollection<PPStrings>();
private class PPStrings
{
public string TxtType { get; set; }
public string SmalPPTxt { get; set; }
public string LargePPTxt { get; set; }
}
private void AddText()
{
PPStrings p1 = new PPStrings();
p1.TxtType = "Text Type";
p1.SmalPPTxt = "LOLXD a dork with small pp.";
p1.LargePPTxt = "A dork with large PP.";
PPStrings p2 = new PPStrings();
p2.TxtType = "Text Type";
p2.SmalPPTxt = "LOLXD a wierdo with small pp.";
p2.LargePPTxt = "A wierdo with large PP.";
PPStrings p3 = new PPStrings();
p3.TxtType = "Text Type";
p3.SmalPPTxt = "HaHa a dork with small pp.";
p3.LargePPTxt = "Haha A dork with large PP.";
StringList.Add(p1);
StringList.Add(p2);
StringList.Add(p3);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.