Showing posts with label wpf. Show all posts
Showing posts with label wpf. Show all posts

Wednesday, January 23, 2013

simple binding wpf listbox to datatable by code / einfaches binden einer wpf listbox an eine datatable mittels code


xaml: add Listbox and Loaded:
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Loaded="Window_Loaded">
           


CodeBehind:
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection("server=192.168.1.231; database=hewmicTest;Trusted_Connection=True");
            sqlCon.Open();
            SqlDataAdapter sqlDa = new SqlDataAdapter("select * from testTable", sqlCon);
            DataTable dt = new DataTable();
            sqlDa.Fill(dt);
            lb.ItemsSource = dt.Rows;
            lb.DisplayMemberPath = "ItemArray[1]";
            sqlCon.Close();
            //MessageBox.Show("ConnState=" + sqlCon.State);
        }

Friday, November 09, 2012

WPF Databinding: über DataContext

z.b.:

im Xaml:

IsEnabled="{Binding Source={StaticResource DataContextBridge}, Path=DataContext.Model.IsLocked}"

im CodeBehind:


        public void Initialize(ImportExportViewModel model, IUnityContainer unityContainer)
        {
            ((FrameworkElement)Resources["DataContextBridge"]).DataContext = DataContext;
DataContext = yourDatModel;
        }