locked
WPF : Translate XAML to Code Behind RRS feed

  • Question

  • How could I write in code behind C# this XAML?

    <Window.Resources>
        <local:DataSetDBWpf x:Key="dataSetDBWpf"/>
        <CollectionViewSource x:Key="clientsViewSource" Source="{Binding Clients, Source={StaticResource dataSetDBWpf}}"/>
        <CollectionViewSource x:Key="clientCommandesViewSource" Source="{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}"/>
    </Window.Resources>

    I am trying to link those two CollectionViewSources by code

    Thank you for your help
    • Edited by rbmoman Wednesday, July 5, 2017 5:25 AM
    Saturday, July 1, 2017 6:56 PM

Answers

  • Thank you Bob for this last link on Brian Noyes blog article. It did answer the issue on how to establish the binding in code behind.

    Best regards

    Raymond

    • Marked as answer by rbmoman Wednesday, July 26, 2017 1:52 PM
    Wednesday, July 26, 2017 1:52 PM

All replies

  • I think you need more explanation for us to help.

    Lloyd Sheen

    Sunday, July 2, 2017 4:31 PM
  • Thank you for your reply.

    This is related to a feedback to Microsoft Support Team: https://connect.microsoft.com/VisualStudio/feedback/details/3136661/ado-net-master-detail-wpf-detail-datagrid-erased-on-rowupdated-event

    And to this post on developpez Forum : https://www.developpez.net/forums/d1712802/dotnet/developpement-windows/windows-presentation-foundation/wpf-traduire-xaml-code-behind/#post9417753

    Thoses CollectionViewSources participate in a Master/Details relationship between two DataViews of a Dataset, and I'm searching a way to declare and define both of them in code behind. That is, get the code instructions that produce what XAML generates by some other ways.

    Especially, I would like to know how to establish the synchronization between the current customer and his orders.

    Hope my explanation be clear enough for you.




    • Edited by rbmoman Wednesday, July 5, 2017 6:42 AM
    Sunday, July 2, 2017 5:57 PM
  • Hi,

    To set  binding in code behind, you need  do as below code.

    CollectionViewSource childCollection = new CollectionViewSource();
    childCollection.Source = dataSetDBWpf;
    Binding binding = new Binding();
    binding.Source = childCollection;
    BindingOperations.SetBinding(ClientsDataGird, DataGrid.ItemsSourceProperty, binding);

    and you can find the dataSetDBWpf form resource in code behind as below.

    this.FindResource("dataSetDBWpf")

    Hope the following articles is helpful  to you.

    Bind WPF controls to a dataset

    https://msdn.microsoft.com/en-us/library/dd547149.aspx

    WPF Data Binding: How to Bind DataSet to WPF Designer(DataSet Master-Details )

    https://blogs.msdn.microsoft.com/vsdata/2009/11/12/wpf-data-binding-how-to-bind-dataset-to-wpf-designer/

    WPF and SQLite Database

    https://www.codeproject.com/Articles/153407/WPF-and-SQLite-Database

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Bob Ding Wednesday, July 5, 2017 7:34 AM
    Monday, July 3, 2017 6:49 AM
  • I've tried this:

    CollectionViewSource childCollection = new CollectionViewSource();
    childCollection.Source = dataSetDBWpf;
    Binding binding = new Binding();
    binding.Source = childCollection;
    BindingOperations.SetBinding("ClientsDataGird", DataGrid.ItemsSourceProperty, binding);

    It did not make the trick! Error CS1503 Argument 1: cannot convert from 'string' to 'System.Windows.DependencyObject'

    Well, what should I do to make it run?

    Thanks

    • Edited by rbmoman Wednesday, July 5, 2017 6:43 AM
    Wednesday, July 5, 2017 3:23 AM
  • Hi,

    I update my reply, please check.

    CollectionViewSource childCollection = new CollectionViewSource();
    childCollection.Source = dataSetDBWpf;
    Binding binding = new Binding();
    binding.Source = childCollection;
    BindingOperations.SetBinding(ClientsDataGird, DataGrid.ItemsSourceProperty, binding);

    How to: Create a Binding in Code

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-create-a-binding-in-code

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 5, 2017 7:33 AM
  • With

    CollectionViewSource childCollection = new CollectionViewSource();
    childCollection.Source = dataSetDBWpf;
    Binding binding = new Binding();
    binding.Source = childCollection;
    BindingOperations.SetBinding(clientsDataGrid, DataGrid.ItemsSourceProperty, binding);

    Getting before

    After execution of code block

    It doesn't work pretty good. The Master DataGrid control gets erased.


    • Edited by rbmoman Friday, July 7, 2017 4:32 AM
    Wednesday, July 5, 2017 3:39 PM
  • Hi,

    From your description, I suggest you can include all necessary code snippets/message/screenshots for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

    This will help us quickly analyze your problem.

    Your understanding and cooperation will be grateful.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 12, 2017 8:17 AM
  • Hello Bob,

    As I said, it didn't work pretty good. So I tried another way :

    CollectionViewSource cvsClients = new CollectionViewSource();
    Binding bdgClients = new Binding();
    bdgClients.Path = new PropertyPath("Clients");
    bdgClients.Source = dataSetDBWpf;
    bdgClients.Mode = BindingMode.TwoWay;
    bdgClients.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);
    clientsViewSource = cvsClients;
    
    CollectionViewSource cvsCommandes = new CollectionViewSource();
    Binding bdgCommandes = new Binding();
    bdgCommandes.Path = new PropertyPath("dataRelationClientCommandes");
    bdgCommandes.Source = clientsViewSource;
    bdgCommandes.Mode = BindingMode.TwoWay;
    bdgCommandes.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(cvsCommandes, CollectionViewSource.SourceProperty, bdgCommandes);
    clientCommandesViewSource = cvsCommandes;

    Getting an execution error on this instruction :

    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);

    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

    Additional information: Une liaison TwoWay ou OneWayToSource ne peut pas fonctionner sur la propriété en lecture seule 'Clients' de type 'DBWpf.DataSetDBWpf'.

    • Edited by rbmoman Thursday, July 13, 2017 10:41 AM
    Wednesday, July 12, 2017 3:43 PM
  • Hi,

    >>A TwoWay or OneWayToSource binding can not work on the read-only property 'Clients' of type 'DBWpf.DataSetDBWpf'.

    The Client property is ReadOnly which may be able to prove why you can not update the detail from the Client when the client have changed.

    I see the questions you submitted on connect, I would continue to focus.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 13, 2017 9:41 AM
  • Ok I tried this way :

    CollectionViewSource cvsClients = new CollectionViewSource();
    Binding bdgClients = new Binding();
    bdgClients.Path = new PropertyPath("Clients");
    bdgClients.Source = dataSetDBWpf;
    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);
    clientsViewSource = cvsClients;
    clientsViewSource.View.CurrentChanged += new System.EventHandler(this.bindingsourceClients_CurrentChanged);
    datarowCurrentClient = ((DataRowView)(clientsViewSource.View.CurrentItem)).Row;
    clientsDataGrid.DataContext = clientsViewSource;
    clientsDataGrid.ItemsSource = clientsViewSource.View;
    
    CollectionViewSource cvsCommandes = new CollectionViewSource();
    Binding bdgCommandes = new Binding();
    bdgCommandes.Path = new PropertyPath("dataRelationClientCommandes");
    bdgCommandes.Source = clientsViewSource.Source;
    BindingOperations.SetBinding(cvsCommandes, CollectionViewSource.SourceProperty, bdgCommandes);
    clientCommandesViewSource = cvsCommandes;
    clientCommandesViewSource.View.CurrentChanged += new System.EventHandler(bindingsourceCommandes_CurrentChanged);
    datarowCurrentCommande = ((DataRowView)(clientCommandesViewSource.View.CurrentItem)).Row;
    commandesDataGrid.DataContext = clientsViewSource;
    commandesDataGrid.ItemsSource = clientCommandesViewSource.View;

    When I execute the code, I am getting this result :

    Before adding a new customer and his order :

    Before saving to the datasource :

    After saving :

    When changing the current customer :

    The Orders details DataGrid is no more synchronized with the current customer in the Customers master DataGrid.

    Could you tell me what am I doing wrong in the binging definitions?

    Thank you for you help

    • Edited by rbmoman Tuesday, July 18, 2017 4:19 AM
    Monday, July 17, 2017 4:53 AM
  • I would appreciate your help

    • Edited by rbmoman Wednesday, July 19, 2017 5:02 AM
    Wednesday, July 19, 2017 12:29 AM
  • Hi Raymond,

    For your previous question,after you UpdateAll for DataSet, do you try to re-load data , as the Window_Load eventy do?

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 19, 2017 6:46 AM
  • Yes I did as given by the designer generated code indeed without any success.

    Hi,

    After you change the dataset, you can try to refresh the dataset by DataSet.AcceptChanges Method.

    WPF and SQLite Database

    https://www.codeproject.com/Articles/153407/WPF-and-SQLite-DatabaseIt is a simply sample for Master-Detail relationship between two DataGrids, you can see  how does it Binding.

    And you can see :WPF DataGrid Edit and Update Access DB https://social.msdn.microsoft.com/Forums/vstudio/en-US/7a459686-9a18-45c2-adf4-9dc7657a94e9/wpf-datagrid-edit-and-update-access-db?forum=wpf

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 19, 2017 7:37 AM
  • Hi Bob, your proposals are off topic.

    • Edited by rbmoman Thursday, July 20, 2017 11:53 PM
    Wednesday, July 19, 2017 5:13 PM
  • Hi Bob and to the remainding of the MSDN Community Support Team,

    Is this issue that difficult to resolve? Because I'm not getting effective answers at this point.

    Would it be too much asking you how to establish the binding in code behind?

    Thank you very much

    • Edited by rbmoman Monday, July 24, 2017 6:23 AM
    Friday, July 21, 2017 1:16 AM
  • Hi,

    Have you tried Microsoft's proposal ? and please be aware that most of the people who answer questions on this forum are volunteers.

    I've seen this exact same question appear multiple times in the forums in the last month, duplicate posting is not allowed.

    If your problem persists, I suggest you listen to the advice of connect.

    If you have any question about wpf Feature, please feel free to contact us.

    For your question: Would it be too much asking you how to establish the binding in code behind?

    See:

    https://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.110).aspx

    You can think of a collection view as the layer on top of the binding source collection .And Views do not change the underlying source collections, each source collection can have multiple views associated with it.

    And please refer the following article:

    http://briannoyesblog.azurewebsites.net/2014/02/20/programmatic-wpf-bindings-and-chaining-collectionviewsource/

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regard,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Bob Ding Thursday, July 27, 2017 1:09 AM
    Wednesday, July 26, 2017 9:43 AM
  • Thank you Bob for this last link on Brian Noyes blog article. It did answer the issue on how to establish the binding in code behind.

    Best regards

    Raymond

    • Marked as answer by rbmoman Wednesday, July 26, 2017 1:52 PM
    Wednesday, July 26, 2017 1:52 PM