WPF Data Binding won't work
Posted
by Tokk
on Stack Overflow
See other posts from Stack Overflow
or by Tokk
Published on 2010-05-31T16:35:18Z
Indexed on
2010/05/31
19:03 UTC
Read the original article
Hit count: 661
Hey, I have got an UserControll with a DependencyProperty called "Risikobewertung" whitch has the own Datatype "RisikoBewertung"(Datatype created by LINQ). So in my Controll I try to bind the Fields of RisikoBewertung to the TextBoxes on the Controll, but It won't work. I hope you can help me, and tell me why ;)
Code: UserControl.xaml:
<UserControl x:Class="Cis.Modules.RiskManagement.Views.Controls.RisikoBewertungEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gridtools="clr-namespace:TmgUnity.Common.Presentation.Controls.DataGridTools;assembly=TmgUnity.Common.Presentation"
xmlns:converter="clr-namespace:Cis.Modules.RiskManagement.Views.Converter"
xmlns:tmg="clr-namespace:TmgUnity.Common.Presentation.Controls.FilterDataGrid;assembly=TmgUnity.Common.Presentation"
xmlns:validators="clr-namespace:TmgUnity.Common.Presentation.ValidationRules;assembly=TmgUnity.Common.Presentation"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:risikoControls="clr-namespace:Cis.Modules.RiskManagement.Views.Controls">
<UserControl.Resources>
<converter:CountToArrowConverter x:Key="CountConverter" />
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Name="Veränderung"/>
<ColumnDefinition Name="Volumen" />
<ColumnDefinition Name="Schadenshöhe" />
<ColumnDefinition Name="SchadensOrte" />
<ColumnDefinition Name="Wahrscheinlichkeit" />
<ColumnDefinition Name="Kategorie" />
<ColumnDefinition Name="Handlungsbedarf" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition />
</Grid.RowDefinitions>
<Image Source="{Binding Path=Entwicklung, Converter={StaticResource CountConverter}, UpdateSourceTrigger=PropertyChanged}" Grid.RowSpan="2" Grid.Row="0" Width="68" Height="68" Grid.Column="0" />
<TextBox Grid.Column="1" Grid.Row="0" Text="Volumen" />
<TextBox Grid.Column="1" Grid.Row="1">
<TextBox.Text>
<Binding Path="Volumen" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
<TextBox Grid.Column="2" Grid.Row="0" Text="Schadenshöhe" />
<TextBox Grid.Column="2" Grid.Row="1" Text="{Binding Path=Schadenshöhe, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Grid.Column="3" Grid.RowSpan="2" Grid.Row="0" Orientation="Horizontal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Text ="Politik" Grid.Row="0" Grid.Column="0"/>
<CheckBox Name="Politik" Grid.Row="1" Grid.Column="0" IsChecked="{Binding Path=Politik, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<TextBox Text ="Vermögen" Grid.Row="0" Grid.Column="1" />
<CheckBox Name="Vermögen" Grid.Row="1" Grid.Column="1" IsChecked="{Binding Path=Vermögen, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" />
<TextBox Text ="Vertrauen" Grid.Row="0" Grid.Column="2" />
<CheckBox Name="Vertrauen" Grid.Row="1" Grid.Column="2" IsChecked="{Binding Path=Vertrauen, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</StackPanel>
<TextBox Grid.Column="4" Grid.Row="0" Text="Wahrscheinlichkeit" />
<TextBox Grid.Column="4" Grid.Row="1" Text="{Binding Path=Wahrscheinlichkeit, UpdateSourceTrigger=PropertyChanged}"/>
<risikoControls:RiskTrafficLightControl Grid.Column="5" Grid.Row="0" Grid.RowSpan="2" RiskValue="{Binding Path=Kategorie, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<StackPanel Grid.Column="6" Grid.RowSpan="2" Grid.Row="0" Orientation="Vertical">
<TextBox Text="Handlungsbedarf" />
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding Path=Handlungsbedarf, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Grid>
The CodeBehind:
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;
using System.ComponentModel;
using Cis.Modules.RiskManagement.Data;
using Cis.Modules.RiskManagement.Views.Models;
namespace Cis.Modules.RiskManagement.Views.Controls
{
/// <summary>
/// Interaktionslogik für RisikoBewertungEditor.xaml
/// </summary>
public partial class RisikoBewertungEditor : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static readonly DependencyProperty RisikoBewertungProperty = DependencyProperty.Register("RisikoBewertung", typeof(RisikoBewertung), typeof(RisikoBewertungEditor), new PropertyMetadata(null, new PropertyChangedCallback(RisikoBewertungChanged)));
// public static readonly DependencyProperty Readonly = DependencyProperty.Register("EditorReadonly", typeof(Boolean), typeof(RisikoBewertungEditor), new PropertyMetadata(null, new PropertyChangedCallback(ReadonlyChanged)));
private static void RisikoBewertungChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs arguments)
{
var bewertungEditor = dependencyObject as RisikoBewertungEditor;
bewertungEditor.RisikoBewertung = arguments.NewValue as RisikoBewertung;
}
/*
private static void ReadonlyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs arguments)
{
}
*/
public RisikoBewertung RisikoBewertung
{
get
{
return GetValue(RisikoBewertungProperty) as RisikoBewertung;
}
set
{
SetValue(RisikoBewertungProperty, value);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("RisikoBewertung"));
}
}
}
/*
public Boolean EditorReadonly
{
get;
set;
}
*/
public void mebosho(object sender, RoutedEventArgs e)
{
MessageBox.Show(RisikoBewertung.LfdNr.ToString());
}
public RisikoBewertungEditor()
{
InitializeComponent();
RisikoBewertung = new RisikoBewertung();
this.DataContext = (GetValue(RisikoBewertungProperty) as RisikoBewertung);
}
}
}
and a little example of it's usage:
<tmg:FilterDataGrid Grid.Row="0" AutoGenerateColumns="False"
ItemsSource="{Binding TodoListe}"
IsReadOnly="False" x:Name="TodoListeDataGrid"
CanUserAddRows="False" SelectionUnit="FullRow"
SelectedValuePath="."
SelectedValue="{Binding CurrentTodoItem}"
gridtools:DataGridStyle.SelectAllButtonTemplate="{DynamicResource CisSelectAllButtonTemplate}"
CanUserResizeColumns="True" MinHeight="80"
SelectionChanged="SelectionChanged"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
diagnostics:PresentationTraceSources.TraceLevel="High"
>
<tmg:FilterDataGrid.RowDetailsTemplate>
<DataTemplate>
<risikoControls:RisikoBewertungEditor x:Name="BewertungEditor" RisikoBewertung="{Binding ElementName=TodoListeDataGrid, Path=SelectedValue}" diagnostics:PresentationTraceSources.TraceLevel="High">
</risikoControls:RisikoBewertungEditor>
</DataTemplate>
</tmg:FilterDataGrid.RowDetailsTemplate>
<tmg:FilterDataGrid.Columns>
<toolkit:DataGridTextColumn Binding="{Binding Path=LfdNr}" Header="LfdNr" />
</tmg:FilterDataGrid.Columns>
</tmg:FilterDataGrid>
© Stack Overflow or respective owner