Archive for May, 2012
Class Set as DataContext in XAML without Code Behind
Posted by Kelly's Chronicles in .NET, C#, XAML on May 6, 2012
In dealing with a problem at work and I am probably as guilty as anyone of relying on code behind to do basic functions without soley using XAML. Why? Because that’s the way I have always done it. But it’s a new day and time to learn new ways to do things. So here we go….
This example shows a class set as datacontext – the code behind file is completely empty.
Have a great week….
<Window x:Class=”cSharpTest.MainWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:vm=”clr-namespace:cSharpTest”
Title=”MainWindow” Height=”350″ Width=”525″>
<Window.Resources>
<vm:MyData x:Key=”ViewModel”/>
</Window.Resources>
<Grid DataContext=”{StaticResource ViewModel}”>
<ListBox Name=”MyListBox” ItemsSource=”{Binding Primes}”/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cSharpTest
{
class MyData
{
public MyData()
{
_primes = new int[5] { 1, 3, 5, 7, 11 };
}
private int[] _primes;
public int[] Primes
{
get { return this._primes; }
}
}
}
BEHIND, c#, Class, CODE, Collections, csharp, DataContext, example, Generic, Grid, ItemsSource, Linq, ListBox, Microsoft, Name, namespace, Presentation, resources, schemas, StaticResource, System, Text, Title, ViewModel, Width, XAML, xmlns, _primes
-
You are currently browsing the archives for May, 2012
Archives
- April 2018
- January 2018
- December 2017
- June 2017
- May 2017
- May 2016
- April 2016
- September 2015
- July 2014
- June 2014
- November 2012
- October 2012
- June 2012
- May 2012
- April 2012
- January 2012
- September 2011
- August 2011
- July 2011
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- August 2008
- July 2008
- June 2008
- May 2008
Recent Comments