To use "using" you need to have implementation IDisposable interface, so here is how my ViewModel look like:
namespace UsingExample.ViewModel { using System; using System.Windows; public class UsingExampleViewModel { public string UsingExampleTest { get; set; } public UsingExampleViewModel() { using (UsingExampleClassTest aaa = new UsingExampleClassTest()) { aaa.myTest = "rrr"; } UsingExampleTest = "test"; } } public class UsingExampleClassTest: IDisposable { public string myTest { get; set; } public void Dispose() { MessageBox.Show("Die object!"); } } }
XAML:
<Window x:Class="UsingExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:UsingExample.ViewModel" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.Resources> <viewModel:UsingExampleViewModel x:Key="UsingExampleViewModel" /> </Grid.Resources> <TextBox DataContext="{StaticResource UsingExampleViewModel}" Text="{Binding UsingExampleTest}"/> </Grid> </Window>
Example project you can download from here.