English 中文(简体)
WPF 数据 束缚性的工作
原标题:WPF Data Binding won t work

简而言之,我拥有一个称为“Risikobewertung”的自有数据型“RisikoBewertung”(由LINQ创建的Data型)。 因此,在我的控制下,我试图将Rysiko Bewertung的田地与控制区的文本Boxes捆绑起来,但赢得了t工作。 我希望你能够帮助我,并告诉我,为什么:

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 Code Behind:

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>
最佳回答

问题是,在您用户控制的构造中,你正在制造一个新物体,并把它作为<代码>DataContext/code>,后来,在您将“RisikoBewertung财产设定为XAML的其他财产时,“DataContext仍被放在其他空物体上。 您需要做的是,将用户控制代码<>DataContext约束到代码上。 与此类似:

<UserControl ...
   DataContext="
        {Binding RelativeSource={RelativeSource Self}, 
                 Path=RisikoBewertung}"
.../>

InitializeComponent();外,从构造中删除所有内容。

public RisikoBewertungEditor()
{
    InitializeComponent();
}
问题回答

我看不出你来了。 Grid s ItemsSource to any any.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签