嗨,我有一個UserControll與DependencyProperty稱爲「Risikobewertung」具有自己的數據類型「RisikoBewertung」(由LINQ創建的數據類型)。所以在我的Controll中,我嘗試將RisikoBewertung的字段綁定到Controll上的TextBoxes,但它不起作用。我希望你能幫助我,並告訴我,爲什麼;)WPF數據綁定將不起作用
代碼: 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>
代碼隱藏:
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);
}
}
}
和一個小例子它的用法:
<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>
數據網格工作PERFEKT,我不認爲這就是問題。 – Tokk 2010-05-31 17:37:15
我同意。我也有同樣的錯誤,並在其他MS Windows上沒有錯誤。看起來有些DLL丟失或類似SL4-SL5發生... – 2012-03-23 14:34:05