Was sind DEPENDENCY Eigenschaften

 

 


MainWindow.xaml:

<Window x:Class="DependencyProperties1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DependencyProperties1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding ElementName=inputBox, Path=FileName}" FontWeight="Bold" />
<local:FileInputBox x:Name="inputBox" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</Window>

 

FileInputBox.xaml:

<UserControl x:Class="DependencyProperties1.FileInputBox"
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"
xmlns:local="clr-namespace:DependencyProperties1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Horizontal" Margin="-94,0,-168,0">
<TextBox Name="tbFileName" Width="350" Height="40" />
<Button Name="btnSelectFile" Content="Öffnen ..." Width=" 100" Height="25" Click="btnSelectFile_Click" />
</StackPanel>
</UserControl>

 

FileInputBox.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using Microsoft.Win32;

namespace DependencyProperties1
{
/// <summary>
/// Interaktionslogik für FileInputBox.xaml
/// </summary>
public partial class FileInputBox : UserControl
{
public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(FileInputBox), new UIPropertyMetadata(string.Empty, TextChangedCallback));


private static void TextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FileInputBox fileInputBox = (FileInputBox)d;
fileInputBox.tbFileName.Text = e.NewValue as string;

}

public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}

public FileInputBox()
{
InitializeComponent();
}

private void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();

if (openFileDialog.ShowDialog() != false)
{
FileName = openFileDialog.FileName;
}

}
}
}

Top