I wrote customcontrol. This is a text box with a button that opens OpenFileDialog.
The Text property for the TextBox is tied to the FileName dependency property. And if the user selects the file through OpenFileDialog, I set the result to this property.
TextBox gets the correct value through binding.
But now my problem. In my opinion, I am using ViewModel. That way, I have a binding to my DependencyProperty "FileName" for the property in my ViewModel. After changing the "FileName" property (changing directly to a text field or selecting a file in a dialog box), the viewmodel property is not updated.
CustomControl.xaml.cs
using System.ComponentModel; using System.Windows; using System.Windows.Controls; using Microsoft.Win32; namespace WpfApplication1.CustomControl {
CustomControl.xaml
<UserControl x:Class="WpfApplication1.CustomControl.FileSelectorTextBox" 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="23" d:DesignWidth="300"> <Border BorderBrush="#FF919191" BorderThickness="0"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" MinWidth="80" /> <ColumnDefinition Width="30" /> </Grid.ColumnDefinitions> <TextBox Name="txtFileName" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="0" Text="{Binding FileName}" /> <Button Name="btnBrowse" Click="btnBrowse_Click" HorizontalContentAlignment="Center" ToolTip="Datei auswählen" Margin="1,0,0,0" Width="29" Padding="1" Grid.Column="1"> <Image Source="../Resources/viewmag.png" Width="15" Height="15" /> </Button> </Grid> </Border> </UserControl>
Usage in view:
<Window x:Class="WpfApplication1.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:WpfApplication1.ViewModels" xmlns:controls="clr-namespace:WpfApplication1.CustomControl" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <vm:MainViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="10" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <DataGrid ItemsSource="{Binding Files}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTemplateColumn Header="File name" Width="*"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <controls:FileSelectorTextBox FileName="{Binding .}" Height="30" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> <ListBox ItemsSource="{Binding Files}" Grid.Row="2"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window>
And ViewModel:
using System.Collections.ObjectModel; using System.ComponentModel; namespace WpfApplication1.ViewModels { internal class MainViewModel : INotifyPropertyChanged { public MainViewModel() { Files = new ObservableCollection<string> { "test1.txt", "test2.txt", "test3.txt", "test4.txt" }; } #region Properties private ObservableCollection<string> _files; public ObservableCollection<string> Files { get { return _files; } set { _files = value; OnPropertyChanged("Files"); } } #endregion Properties #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion INotifyPropertyChanged Members } }
Is there any misuse of the dependency property? Note. The problem only occurs in the DataGrid.
source share