Is there a way to bind to a UWP TreeView that may have a variable number of levels like a folder structure...
Basically I want to use TreeView to represent a folder structure. So there maybe be any number of subfolders to navigate into. Can this be done via databinding, or does such logic have to be done all in code? Thanks.
c# xaml visual-c++ uwp
add a comment |
Basically I want to use TreeView to represent a folder structure. So there maybe be any number of subfolders to navigate into. Can this be done via databinding, or does such logic have to be done all in code? Thanks.
c# xaml visual-c++ uwp
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23
add a comment |
Basically I want to use TreeView to represent a folder structure. So there maybe be any number of subfolders to navigate into. Can this be done via databinding, or does such logic have to be done all in code? Thanks.
c# xaml visual-c++ uwp
Basically I want to use TreeView to represent a folder structure. So there maybe be any number of subfolders to navigate into. Can this be done via databinding, or does such logic have to be done all in code? Thanks.
c# xaml visual-c++ uwp
c# xaml visual-c++ uwp
asked Nov 23 '18 at 1:11
LeBrown Jones
223212
223212
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23
add a comment |
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23
add a comment |
1 Answer
1
active
oldest
votes
The TreeView control supports binding to a hierarchical data source. You could define a custom class for binding usage.
I made a code sample for you reference:
<Grid>
<TreeView x:Name="treeview" ItemsSource="{x:Bind storageFolders,Mode=OneWay}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="folders" Click="Button_Click"></Button>
</Grid>
public class FolderInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
RaisePropertyChanged("FolderName");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add(".txt");
var folder = await folderPicker.PickSingleFolderAsync();
var Folders = await GetFoldersAsync(folder);
foreach (var f in Folders)
{
storageFolders.Add(f);
}
}
private async Task<ObservableCollection<FolderInfo>> GetFoldersAsync(StorageFolder storageFolder)
{
var folders = await storageFolder.GetFoldersAsync();
ObservableCollection<FolderInfo> folderInfos = new ObservableCollection<FolderInfo>();
foreach (var f in folders)
{
folderInfos.Add(new FolderInfo() {FolderName=f.DisplayName,subFolders=await GetFoldersAsync(f) });
}
return folderInfos;
}
}
Then, when you have new sub folders, you just need to add it to the storageFolders
collection.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439595%2fis-there-a-way-to-bind-to-a-uwp-treeview-that-may-have-a-variable-number-of-leve%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The TreeView control supports binding to a hierarchical data source. You could define a custom class for binding usage.
I made a code sample for you reference:
<Grid>
<TreeView x:Name="treeview" ItemsSource="{x:Bind storageFolders,Mode=OneWay}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="folders" Click="Button_Click"></Button>
</Grid>
public class FolderInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
RaisePropertyChanged("FolderName");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add(".txt");
var folder = await folderPicker.PickSingleFolderAsync();
var Folders = await GetFoldersAsync(folder);
foreach (var f in Folders)
{
storageFolders.Add(f);
}
}
private async Task<ObservableCollection<FolderInfo>> GetFoldersAsync(StorageFolder storageFolder)
{
var folders = await storageFolder.GetFoldersAsync();
ObservableCollection<FolderInfo> folderInfos = new ObservableCollection<FolderInfo>();
foreach (var f in folders)
{
folderInfos.Add(new FolderInfo() {FolderName=f.DisplayName,subFolders=await GetFoldersAsync(f) });
}
return folderInfos;
}
}
Then, when you have new sub folders, you just need to add it to the storageFolders
collection.
add a comment |
The TreeView control supports binding to a hierarchical data source. You could define a custom class for binding usage.
I made a code sample for you reference:
<Grid>
<TreeView x:Name="treeview" ItemsSource="{x:Bind storageFolders,Mode=OneWay}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="folders" Click="Button_Click"></Button>
</Grid>
public class FolderInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
RaisePropertyChanged("FolderName");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add(".txt");
var folder = await folderPicker.PickSingleFolderAsync();
var Folders = await GetFoldersAsync(folder);
foreach (var f in Folders)
{
storageFolders.Add(f);
}
}
private async Task<ObservableCollection<FolderInfo>> GetFoldersAsync(StorageFolder storageFolder)
{
var folders = await storageFolder.GetFoldersAsync();
ObservableCollection<FolderInfo> folderInfos = new ObservableCollection<FolderInfo>();
foreach (var f in folders)
{
folderInfos.Add(new FolderInfo() {FolderName=f.DisplayName,subFolders=await GetFoldersAsync(f) });
}
return folderInfos;
}
}
Then, when you have new sub folders, you just need to add it to the storageFolders
collection.
add a comment |
The TreeView control supports binding to a hierarchical data source. You could define a custom class for binding usage.
I made a code sample for you reference:
<Grid>
<TreeView x:Name="treeview" ItemsSource="{x:Bind storageFolders,Mode=OneWay}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="folders" Click="Button_Click"></Button>
</Grid>
public class FolderInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
RaisePropertyChanged("FolderName");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add(".txt");
var folder = await folderPicker.PickSingleFolderAsync();
var Folders = await GetFoldersAsync(folder);
foreach (var f in Folders)
{
storageFolders.Add(f);
}
}
private async Task<ObservableCollection<FolderInfo>> GetFoldersAsync(StorageFolder storageFolder)
{
var folders = await storageFolder.GetFoldersAsync();
ObservableCollection<FolderInfo> folderInfos = new ObservableCollection<FolderInfo>();
foreach (var f in folders)
{
folderInfos.Add(new FolderInfo() {FolderName=f.DisplayName,subFolders=await GetFoldersAsync(f) });
}
return folderInfos;
}
}
Then, when you have new sub folders, you just need to add it to the storageFolders
collection.
The TreeView control supports binding to a hierarchical data source. You could define a custom class for binding usage.
I made a code sample for you reference:
<Grid>
<TreeView x:Name="treeview" ItemsSource="{x:Bind storageFolders,Mode=OneWay}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="local:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button Content="folders" Click="Button_Click"></Button>
</Grid>
public class FolderInfo : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(PropertyName));
}
}
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
RaisePropertyChanged("FolderName");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
public sealed partial class MainPage : Page
{
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
FolderPicker folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
folderPicker.FileTypeFilter.Add(".txt");
var folder = await folderPicker.PickSingleFolderAsync();
var Folders = await GetFoldersAsync(folder);
foreach (var f in Folders)
{
storageFolders.Add(f);
}
}
private async Task<ObservableCollection<FolderInfo>> GetFoldersAsync(StorageFolder storageFolder)
{
var folders = await storageFolder.GetFoldersAsync();
ObservableCollection<FolderInfo> folderInfos = new ObservableCollection<FolderInfo>();
foreach (var f in folders)
{
folderInfos.Add(new FolderInfo() {FolderName=f.DisplayName,subFolders=await GetFoldersAsync(f) });
}
return folderInfos;
}
}
Then, when you have new sub folders, you just need to add it to the storageFolders
collection.
edited Nov 28 '18 at 5:36
answered Nov 28 '18 at 3:14
Xavier Xie - MSFT
4,9261315
4,9261315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439595%2fis-there-a-way-to-bind-to-a-uwp-treeview-that-may-have-a-variable-number-of-leve%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can this be done via databinding yes
– Selvin
Nov 23 '18 at 1:27
Thanks for reply. Can I maybe get a HINT of a clue for kicking this off? I can easily bind to a collection of items in XAML, but I don’t know what XAML markup is used to bind to a unknown number of sub folders
– LeBrown Jones
Nov 23 '18 at 1:31
official UWP TreeView documentation should be a good start
– Selvin
Nov 23 '18 at 1:32
You're looking for a tree view with hierarchical template.
– jsanalytics
Nov 23 '18 at 10:23