Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

UserControl Binding With DependencyProperty

$
0
0

I want to bind the Properties.Settings.Default properties directly to a UserControl property. My problem is that the binding only works in one direction. If I want to save the settings I need to assign every property manually to the Properties.Settings.Defaultproperties. Is there a better way to achieve this?

<UserControl x:Class="WPFUserControlTestWithBinding.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"><StackPanel><Label>Text:</Label><TextBox Text="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}, Path=ConnectionString}"></TextBox></StackPanel></UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFUserControlTestWithBinding
{
    /// <summary>
    /// Interaktionslogik für UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string ConnectionString
        {
            get { return (string)GetValue(ConnectionStringProperty); }
            set { SetValue(ConnectionStringProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ConnectionString.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ConnectionStringProperty =
            DependencyProperty.Register("ConnectionString", typeof(string), typeof(UserControl1), new UIPropertyMetadata("DataSource=test"));


    }
}

MainWindow.xaml
<Window x:Class="WPFUserControlTestWithBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:test="clr-namespace:WPFUserControlTestWithBinding"
        Title="MainWindow" Height="350" Width="525"><StackPanel><test:UserControl1 x:Name="Usercontrolinstance" ConnectionString="{Binding MyConnectionString}"/><Button Content="Speichern" Click="Button_Click" /></StackPanel></Window>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WPFUserControlTestWithBinding
{    /// <summary>    /// Interaktionslogik für MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            this.DataContext = Properties.Settings.Default;        }        private void Button_Click(object sender, RoutedEventArgs e)        {            // I don't want to make this step....            Properties.Settings.Default.MyConnectionString = Usercontrolinstance.ConnectionString;            // ... but simply call this one.            Properties.Settings.Default.Save();        }    }
}



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>