2014-11-20 90 views
0

我在另一個網格內部有一個網格,都綁定到一個模型,父網格工作正常,但子網格綁定沒有,我找不到錯誤,這是對象:WPF DataBinding不起作用

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml.Schema; 
using System.Xml.Serialization; 

namespace SAE_Connector 
{ 
[XmlRoot("Configuracion")] 
public class clsConfig 
{ 
    [XmlElement] 
    public Config config { get; set; } 

    [XmlArrayItem] 
    public List<PrePoliza> PrePolizas { get; set; } 

    public clsConfig() 
    { 
     config = new Config(); 
     PrePolizas = new List<PrePoliza>(); 
    } 
} 

public class Config 
{ 
    [XmlAttribute] 
    public string RutaDB { get; set; } 

    public Config() 
    { 
     Impuestos = new clsImpuestos(); 
    } 

    private string _numEmp = ""; 
    [XmlAttribute] 
    public string NumEmp 
    { 
     get 
     { 
      return NumeroEmpresaGet(); 
     } 
     set 
     { 
      _numEmp = NumEmp; 
     } 
    } 

    [XmlAttribute] 
    public string InstanciaCompacSQL { get; set; } 

    [XmlAttribute] 
    public string EmpresaContPAQ { get; set; } 

    [XmlAttribute] 
    public string PassWord { get; set; } 

    [XmlElement("Impuestos")] 
    public clsImpuestos Impuestos { get; set; } 

    private string NumeroEmpresaGet() 
    { 
     string _result = ""; 
     string _emp = string.IsNullOrEmpty(RutaDB) == false ? Path.GetFileNameWithoutExtension(RutaDB) : string.Empty; 
     if(string.IsNullOrEmpty(_emp)==false) 
     { 
      //SAE50EMPRE02 
      _result = _emp.Substring(_emp.Length - 2); 
     } 
     return _result; 
    } 
} 

#region impuestos 
    public class clsImpuestos 
    { 
    [XmlElement("Impuesto1")]  
    public clsImpuesto1 impuesto1 { get; set; } 
    [XmlElement("Impuesto2")] 
    public clsImpuesto2 impuesto2 { get; set; } 
    [XmlElement("Impuesto3")] 
    public clsImpuesto3 impuesto3 { get; set; } 
    [XmlElement("Impuesto4")] 
    public clsImpuesto4 impuesto4 { get; set; } 

    public clsImpuestos() 
    { 
     impuesto1 = new clsImpuesto1(); 
     impuesto2 = new clsImpuesto2(); 
     impuesto3 = new clsImpuesto3(); 
     impuesto4 = new clsImpuesto4(); 
    } 
    } 

    public class clsImpuesto1 
    { 
     [XmlAttribute] 
     public string Nombre { get; set; } 
     [XmlAttribute] 
     public double Tasa { get; set; } 
    }  
    public class clsImpuesto2 
    { 
     [XmlAttribute] 
     public string Nombre { get; set; } 
     [XmlAttribute] 
     public double Tasa { get; set; } 
    }  
    public class clsImpuesto3 
    { 
     [XmlAttribute] 
     public string Nombre { get; set; } 
     [XmlAttribute] 
     public double Tasa { get; set; } 
    }  
    public class clsImpuesto4 
    { 
     [XmlAttribute] 
     public string Nombre { get; set; } 
     [XmlAttribute] 
     public double Tasa { get; set; } 
    } 
#endregion 

public enum eTipoPol { Ingresos, Egresos, Diario } 

public class PrePoliza 
{ 
    [XmlAttribute] 
    public int Id { get; set; } 
    [XmlAttribute] 
    public eTipoPol tipoPol { get; set; } 
    [XmlAttribute] 
    public string Nombre { get; set; } 
    [XmlAttribute] 
    public string Concepto { get; set; } 
    [XmlArrayItem] 
    public List<Cuenta> Cuentas { get; set; } 

    public enum eCargoAbono { Cargo, Abono } 
    public enum eProporcion { SubTotal, Impuesto1, Impuesto2, Impuesto3, Impuesto4, Total } 
    public enum eReferenciaConcepto { Vacio, Observaciones, Folio, Texto } 

    public PrePoliza() 
    { 
     Cuentas = new List<Cuenta>(); 
    } 

    public class Cuenta 
    { 
     [XmlAttribute] 
     public int Id { get; set; } 
     [XmlAttribute] 
     public string CuentaCod { get; set; } 
     [XmlAttribute] 
     public string Descripcion { get; set; } 
     [XmlAttribute] 
     public bool EsCuentaCte { get; set; } 
     [XmlAttribute] 
     public eCargoAbono CargoAbono { get; set; } 
     [XmlAttribute] 
     public eProporcion Proporcion { get; set; } 
     [XmlAttribute] 
     public eReferenciaConcepto Referencia { get; set; } 
     [XmlAttribute] 
     public string RefText { get; set; } 
     [XmlAttribute] 
     public eReferenciaConcepto Concepto { get; set; } 
     [XmlAttribute] 
     public string ConcText { get; set; } 
    } 
} 
} 

這是完整的XAML

<Window x:Class="wpfSaePolizas.xWindows.wPrePolizas" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Pre-Polizas" Height="600" Width="1000" Background="{StaticResource brBlue}" WindowState="Maximized" Loaded="Window_Loaded" > 
<Grid> 
    <Grid.Resources> 
     <Style TargetType="TextBlock" BasedOn="{StaticResource xTextBlockWhiteS}" > 
      <Setter Property="FontWeight" Value="Normal" /> 
     </Style> 
     <Style TargetType="TextBox"> 
      <Setter Property="Template" Value="{StaticResource TextBoxRoundCorners}" /> 
     </Style> 
     <Style TargetType="Border"> 
      <Setter Property="CornerRadius" Value="5" /> 
     </Style> 
    </Grid.Resources> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="70" /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 

    <Border Grid.Row="0" BorderThickness="2" BorderBrush="{StaticResource brBorder}" Margin="5" > 
     <Border.Resources> 
      <Style TargetType="StackPanel" > 
       <Setter Property="Orientation" Value="Horizontal" /> 
      </Style> 
      <Style TargetType="Image"> 
       <Setter Property="Height" Value="20" /> 
      </Style> 
     </Border.Resources> 
     <ToolBar Margin="5" Background="Transparent"> 
      <Button Name="btnNva" Background="#FFFFD801" Width="90" Height="30" Click="btnNva_Click" > 
       <Button.Content> 
        <StackPanel> 
         <Image Source="..\img\Add1.png" /> 
         <TextBlock Text="Nueva" /> 
        </StackPanel> 
       </Button.Content> 
      </Button> 

      <Button Name="btnEliminar" Background="#FFFFD801" Width="90" Height="30" Click="btnEliminar_Click" > 
       <Button.Content> 
        <StackPanel> 
         <Image Source="..\img\Del.png" /> 
         <TextBlock Text="Eliminar" /> 
        </StackPanel> 
       </Button.Content> 
      </Button> 

      <Button Name="btnGuardar" Background="#FFFFD801" Width="90" Height="30" Click="btnGuardar_Click" > 
       <Button.Content> 
        <StackPanel> 
         <Image Source="..\img\Save.png" /> 
         <TextBlock Text="Guardar" /> 
        </StackPanel> 
       </Button.Content> 
      </Button> 

      <Button Name="btnSalir" Background="{StaticResource gradGris}" Width="90" Height="30" Click="btnSalir_Click"> 
       <Button.Content> 
        <StackPanel> 
         <TextBlock Text="Salir" /> 
        </StackPanel> 
       </Button.Content> 
      </Button> 
     </ToolBar> 
    </Border> 

    <Grid Grid.Row="1"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="400" /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 

     <DataGrid Name="dgCuentas" Grid.Column="1" Background="Transparent" AutoGenerateColumns="True" ></DataGrid> 

     <Border Name="brConfig" Grid.Column="0" BorderThickness="2" BorderBrush="{StaticResource brBorder}" Margin="5" > 
      <Viewbox VerticalAlignment="Top" Margin="5"> 
       <Grid Name="gridPrePoliza"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="1" /> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 

        <TextBlock Text="Pre-Póliza:" Grid.Row="0" Grid.Column="0" /> 
        <ComboBox Name="cmbPrePolizas" Grid.Row="0" Grid.Column="1" Margin="5" SelectionChanged="cmbPrePolizas_SelectionChanged" > 
         <ComboBox.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding Nombre}" /> 
          </DataTemplate> 
         </ComboBox.ItemTemplate> 
        </ComboBox> 

        <TextBlock Text="Nombre:" Grid.Row="1" Grid.Column="0" /> 
        <TextBox Name="txtNombre" Grid.Row="1" Grid.Column="1" Text="{Binding Nombre, UpdateSourceTrigger=PropertyChanged}" /> 

        <TextBlock Text="Tipo de Póliza:" Grid.Row="2" Grid.Column="0" /> 
        <ComboBox Name="cmbTiposPoliza" Grid.Row="2" Grid.Column="1" Margin="5" > 
         <ComboBox.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Text="{Binding }" /> 
          </DataTemplate> 
         </ComboBox.ItemTemplate> 
        </ComboBox> 

        <TextBlock Text="Concepto Póliza:" Grid.Row="3" Grid.Column="0" /> 
        <TextBox Name="txtConceptoPol" Grid.Row="3" Grid.Column="1" Text="{Binding Concepto, UpdateSourceTrigger=PropertyChanged}" /> 

        <Grid Name="gridCuenta" Grid.Row="4" Grid.ColumnSpan="2" Margin="0, 20" IsEnabled="False"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="90" /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 

         <TextBlock Text="Cuenta:" Grid.Row="0" Grid.Column="0" /> 
         <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="1"> 
          <TextBox Name="Cuenta" Width="157" Text="{Binding Path=CuentaCod, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" TextChanged="Cuenta_TextChanged"/> 
          <Button Name="btnCuentaBuscar" Content="..." Style="{StaticResource MetroButton}" Width="35" Click="btnCuentaBuscar_Click" Margin="0,0,0,3" VerticalAlignment="Bottom" /> 
         </StackPanel> 
         <TextBlock Name="lblCuentaNombre" Text="{Binding Path=Descripcion, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Row="1" Grid.ColumnSpan="2" TextWrapping="Wrap" HorizontalAlignment="Center" /> 
         <CheckBox Name="chbEsCteProv" Grid.Row="2" Grid.ColumnSpan="2"> 
          <CheckBox.Content> 
           <TextBlock Text="Es Cuenta de Cliente/Proveedor" /> 
          </CheckBox.Content> 
         </CheckBox> 

         <TextBlock Text="Cargo/Abono:" Grid.Row="3" Grid.Column="0" /> 
         <ComboBox Name="cmbCargoAbono" Grid.Row="3" Grid.Column="1" Margin="5" SelectionChanged="cmbCargoAbono_SelectionChanged" > 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

         <TextBlock Text="Proporción:" Grid.Row="4" Grid.Column="0" /> 
         <ComboBox Name="cmbProporcion" Grid.Row="4" Grid.Column="1" Margin="5" SelectionChanged="cmbProporcion_SelectionChanged" > 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 

         <TextBlock Text="Referencia:" Grid.Row="5" Grid.Column="0" /> 
         <ComboBox Name="cmbRefer" Grid.Row="5" Grid.Column="1" Margin="5" SelectionChanged="cmbRefer_SelectionChanged" > 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
         <TextBox Name="txtRefer" Grid.Row="6" Grid.ColumnSpan="2" IsEnabled="False" 
           Text="{Binding Path=RefText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

         <TextBlock Text="Concepto:" Grid.Row="7" Grid.Column="0" /> 
         <ComboBox Name="cmbConcepto" Grid.Row="7" Grid.Column="1" Margin="5" SelectionChanged="cmbConcepto_SelectionChanged" > 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
         <TextBox Name="txtConcepto" Grid.Row="8" Grid.ColumnSpan="2" IsEnabled="False" 
           Text="{Binding Path=ConcText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" /> 

        </Grid> 

        <Border Name="brCtasBotones" Grid.Row="5" Grid.ColumnSpan="2" BorderThickness="2" BorderBrush="{StaticResource brBorder}" 
          Padding="5" CornerRadius="5"> 
         <StackPanel Name="stackBotones" Orientation="Horizontal" HorizontalAlignment="Center" IsEnabled="False" > 

          <Button Name="btnNvaCta" Background="#FFFFD801" Width="80" Height="30" Grid.Row="4" Click="btnNvaCta_Click" > 
           <Button.Content> 
            <StackPanel Orientation="Horizontal"> 
             <Image Source="..\img\Nvo.png" Width="18" /> 
             <TextBlock Text="Nueva" /> 
            </StackPanel> 
           </Button.Content>          
          </Button> 

          <Button Name="btnEliminarCta" Background="#FFFFD801" Width="80" Height="30" Grid.Row="4" Click="btnEliminarCta_Click" > 
           <Button.Content> 
            <StackPanel Orientation="Horizontal"> 
             <Image Source="..\img\Del.png" Width="18" /> 
             <TextBlock Text="Borrar" /> 
            </StackPanel> 
           </Button.Content> 
          </Button> 

          <Button Name="btnGuardarCta" Background="#FFFFD801" Width="80" Height="30" Grid.Row="4" Click="btnGuardarCta_Click" > 
           <Button.Content> 
            <StackPanel Orientation="Horizontal"> 
             <Image Source="..\img\Save.png" Width="18" /> 
             <TextBlock Text="Guardar" /> 
            </StackPanel> 
           </Button.Content> 
          </Button> 

         </StackPanel> 
        </Border> 

       </Grid> 
      </Viewbox> 
     </Border> 
    </Grid> 

</Grid> 

這是電網

的DataContext設置爲兒童

這是對的DataContext設置的DataGrid

dgCuentas.ItemsSource = _prepoliza.Cuentas; 

這是UI的窗口:

http://postimg.org/image/ik94x4iwd/

回答

0

您的DataContext設置的對象。 XML屬性在這一點上是無關緊要的。但是,如果您嘗試創建Cuenta並將其放入PrePoliza列表中,則應該使用ObservableCollection<T>而不是List<T>,並且需要將新的_cta添加到父集合中。

如果你綁定到一個DataGrid,每行都會綁定到行的隱式上下文,所以你只需要提供一個簡單的綁定指令。例如:

<Grid> 
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/> 
      <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" /> 
      <!--The Email property contains a URI. For example "mailto:[email protected]"--> 
      <DataGridHyperlinkColumn Header="Email" Binding="{Binding Email}" ContentBinding="{Binding Email, Converter={StaticResource EmailConverter}}" /> 
      <DataGridCheckBoxColumn Header="Member?" Binding="{Binding IsMember}" /> 
      <DataGridComboBoxColumn Header="Order Status" SelectedItemBinding="{Binding Status}" ItemsSource="{Binding Source={StaticResource myEnum}}" /> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

如果您已經遠遠得到,但無法弄清楚如何讓一個TextBox到DataGrid中,那麼也許使用DataGridTemplateColumn

+0

我添加了XML屬性,因爲我將這個對象序列化爲XML用於存儲和檢索數據,我忘了說Parent是綁定到PrePoliza並且這個工作正常,問題與Child Grid有關,Cuenta的實例稍後添加到Prepolizas.Cuentas,但我的問題是如何將控件綁定到Cuenta,任何示例代碼? – jerryeagle 2014-11-20 18:47:04

+0

你想把控件綁定到當前選定的Cuentas上嗎?或者一個GrivView綁定到每個Cuentas的控件? – bigtlb 2014-11-20 18:50:04

+0

也許綁定到PrePoliza.Cuentas集合的[DataGrid](http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.aspx)? – bigtlb 2014-11-20 18:52:28