multi Datacontext wpf











up vote
-1
down vote

favorite












Who can explain to me how to use two different datacontext ?



this is my file.xaml.cs :



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DataContext = new string { "Female", "Male", "Animal", "Safe", "Soft", "Hard", "Space", "Landscape", "Outside", "Inside",
"City", "France", "Flower", "Sunset", "Sky", "Fireworks", "Spring", "Winter", "Summer", "Fall", "Christmas", "Halloween",
"Ghost", "Demon", "Angel", "Watermelon", "Storm", "Waterfall", "Night", "Sun","Moon", "Dog", "Cat", "Food", "Cheese",
"Kancolle", "IT", "UFO", "Travel", "Sport", "Nightmare"};
}


and here my file.xaml :



<ScrollViewer HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="97" Margin="10,149,0,0">
<ListBox ItemsSource="{Binding .}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=.}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>


Everything work well here, but i would like add an second ScrollViewer exactly like the first one, but with another content. so the datacontexte need to be different for each of them.



thank you for giving me a little of your time.










share|improve this question







New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
    – Clemens
    yesterday















up vote
-1
down vote

favorite












Who can explain to me how to use two different datacontext ?



this is my file.xaml.cs :



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DataContext = new string { "Female", "Male", "Animal", "Safe", "Soft", "Hard", "Space", "Landscape", "Outside", "Inside",
"City", "France", "Flower", "Sunset", "Sky", "Fireworks", "Spring", "Winter", "Summer", "Fall", "Christmas", "Halloween",
"Ghost", "Demon", "Angel", "Watermelon", "Storm", "Waterfall", "Night", "Sun","Moon", "Dog", "Cat", "Food", "Cheese",
"Kancolle", "IT", "UFO", "Travel", "Sport", "Nightmare"};
}


and here my file.xaml :



<ScrollViewer HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="97" Margin="10,149,0,0">
<ListBox ItemsSource="{Binding .}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=.}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>


Everything work well here, but i would like add an second ScrollViewer exactly like the first one, but with another content. so the datacontexte need to be different for each of them.



thank you for giving me a little of your time.










share|improve this question







New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
    – Clemens
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Who can explain to me how to use two different datacontext ?



this is my file.xaml.cs :



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DataContext = new string { "Female", "Male", "Animal", "Safe", "Soft", "Hard", "Space", "Landscape", "Outside", "Inside",
"City", "France", "Flower", "Sunset", "Sky", "Fireworks", "Spring", "Winter", "Summer", "Fall", "Christmas", "Halloween",
"Ghost", "Demon", "Angel", "Watermelon", "Storm", "Waterfall", "Night", "Sun","Moon", "Dog", "Cat", "Food", "Cheese",
"Kancolle", "IT", "UFO", "Travel", "Sport", "Nightmare"};
}


and here my file.xaml :



<ScrollViewer HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="97" Margin="10,149,0,0">
<ListBox ItemsSource="{Binding .}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=.}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>


Everything work well here, but i would like add an second ScrollViewer exactly like the first one, but with another content. so the datacontexte need to be different for each of them.



thank you for giving me a little of your time.










share|improve this question







New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Who can explain to me how to use two different datacontext ?



this is my file.xaml.cs :



public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DataContext = new string { "Female", "Male", "Animal", "Safe", "Soft", "Hard", "Space", "Landscape", "Outside", "Inside",
"City", "France", "Flower", "Sunset", "Sky", "Fireworks", "Spring", "Winter", "Summer", "Fall", "Christmas", "Halloween",
"Ghost", "Demon", "Angel", "Watermelon", "Storm", "Waterfall", "Night", "Sun","Moon", "Dog", "Cat", "Food", "Cheese",
"Kancolle", "IT", "UFO", "Travel", "Sport", "Nightmare"};
}


and here my file.xaml :



<ScrollViewer HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="97" Margin="10,149,0,0">
<ListBox ItemsSource="{Binding .}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=.}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>


Everything work well here, but i would like add an second ScrollViewer exactly like the first one, but with another content. so the datacontexte need to be different for each of them.



thank you for giving me a little of your time.







wpf xaml binding datacontext






share|improve this question







New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









astrocurieux

53




53




New contributor




astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






astrocurieux is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
    – Clemens
    yesterday


















  • As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
    – Clemens
    yesterday
















As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
– Clemens
yesterday




As a note, it looks odd that the ListBox is inside a ScrollViewer. A ListBox already provides scrolling by a ScrollViewer in its ControlTemplate.
– Clemens
yesterday












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You don't need to set different DataContexts.



Create two collection properties in your MainWindow class and set the DataContext to the window instance.



public IEnumerable<string> Collection1 { get; }
public IEnumerable<string> Collection2 { get; }

public MainWindow()
{
InitializeComponent();

Collection1 = new string { ... };
Collection2 = new string { ... };
DataContext = this;
}


Bind the ListBox's ItemSource to the collection properties:



<ListBox ItemsSource="{Binding Collection1}" ...>
...
<ListBox ItemsSource="{Binding Collection2}" ...>





share|improve this answer





















  • thank you for your help, it's a simple solution and very comprehenssible.
    – astrocurieux
    yesterday











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',
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
});


}
});






astrocurieux is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237998%2fmulti-datacontext-wpf%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You don't need to set different DataContexts.



Create two collection properties in your MainWindow class and set the DataContext to the window instance.



public IEnumerable<string> Collection1 { get; }
public IEnumerable<string> Collection2 { get; }

public MainWindow()
{
InitializeComponent();

Collection1 = new string { ... };
Collection2 = new string { ... };
DataContext = this;
}


Bind the ListBox's ItemSource to the collection properties:



<ListBox ItemsSource="{Binding Collection1}" ...>
...
<ListBox ItemsSource="{Binding Collection2}" ...>





share|improve this answer





















  • thank you for your help, it's a simple solution and very comprehenssible.
    – astrocurieux
    yesterday















up vote
0
down vote



accepted










You don't need to set different DataContexts.



Create two collection properties in your MainWindow class and set the DataContext to the window instance.



public IEnumerable<string> Collection1 { get; }
public IEnumerable<string> Collection2 { get; }

public MainWindow()
{
InitializeComponent();

Collection1 = new string { ... };
Collection2 = new string { ... };
DataContext = this;
}


Bind the ListBox's ItemSource to the collection properties:



<ListBox ItemsSource="{Binding Collection1}" ...>
...
<ListBox ItemsSource="{Binding Collection2}" ...>





share|improve this answer





















  • thank you for your help, it's a simple solution and very comprehenssible.
    – astrocurieux
    yesterday













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You don't need to set different DataContexts.



Create two collection properties in your MainWindow class and set the DataContext to the window instance.



public IEnumerable<string> Collection1 { get; }
public IEnumerable<string> Collection2 { get; }

public MainWindow()
{
InitializeComponent();

Collection1 = new string { ... };
Collection2 = new string { ... };
DataContext = this;
}


Bind the ListBox's ItemSource to the collection properties:



<ListBox ItemsSource="{Binding Collection1}" ...>
...
<ListBox ItemsSource="{Binding Collection2}" ...>





share|improve this answer












You don't need to set different DataContexts.



Create two collection properties in your MainWindow class and set the DataContext to the window instance.



public IEnumerable<string> Collection1 { get; }
public IEnumerable<string> Collection2 { get; }

public MainWindow()
{
InitializeComponent();

Collection1 = new string { ... };
Collection2 = new string { ... };
DataContext = this;
}


Bind the ListBox's ItemSource to the collection properties:



<ListBox ItemsSource="{Binding Collection1}" ...>
...
<ListBox ItemsSource="{Binding Collection2}" ...>






share|improve this answer












share|improve this answer



share|improve this answer










answered yesterday









Clemens

86.2k884166




86.2k884166












  • thank you for your help, it's a simple solution and very comprehenssible.
    – astrocurieux
    yesterday


















  • thank you for your help, it's a simple solution and very comprehenssible.
    – astrocurieux
    yesterday
















thank you for your help, it's a simple solution and very comprehenssible.
– astrocurieux
yesterday




thank you for your help, it's a simple solution and very comprehenssible.
– astrocurieux
yesterday










astrocurieux is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















astrocurieux is a new contributor. Be nice, and check out our Code of Conduct.













astrocurieux is a new contributor. Be nice, and check out our Code of Conduct.












astrocurieux is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237998%2fmulti-datacontext-wpf%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues