嘿,我有一个名为“Risikobewertung”的DependencyProperty的UserControll,它有自己的数据类型“RisikoBewertung”(由LINQ创建的数据类型)。因此,在我的Controll中,我尝试将RisikoBewertung的Fields绑定到Controll上的TextBoxes,但它不起作用。我希望你能帮助我,并告诉我为什么;)
代码: 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>
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);
}
}
}
以及它的用法的一个小例子:
<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>
答案 0 :(得分:1)
问题是在用户控件的构造函数中,您正在创建一个新对象并将其设置为DataContext
,稍后当您将RisikoBewertung
属性设置为XAML中的其他属性时, DataContext
仍然设置在另一个空对象上。您需要做的是将用户控件的DataContext
绑定到RisikoBewertung
属性。像这样:
<UserControl ...
DataContext="
{Binding RelativeSource={RelativeSource Self},
Path=RisikoBewertung}"
.../>
从InitializeComponent();
public RisikoBewertungEditor()
{
InitializeComponent();
}
答案 1 :(得分:0)
我没有看到您将Grid的ItemsSource
设置为任何内容。