Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining Sub-model window and Sub-model sequence #544

Closed
francotiveron opened this issue Nov 10, 2022 · 7 comments
Closed

Combining Sub-model window and Sub-model sequence #544

francotiveron opened this issue Nov 10, 2022 · 7 comments

Comments

@francotiveron
Copy link

High-level description
The Samples don't seem to include an example where a variable number of Instances of a Window are opened and bound to an element of a collection of models, like in a Sub-model sequence-of-windows binding.

Please provide such a sample or indicate the link if it already exists.

@TysonMN
Copy link
Member

TysonMN commented Nov 10, 2022

What is your use case?

@francotiveron
Copy link
Author

francotiveron commented Nov 10, 2022

I have 2 buttons (or a list of buttons) in the MainWindow. I want to open(close) many ChartWindows clicking on different buttons

<Window x:Class="Yatp.Desktop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Yatp.Desktop"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Command="{Binding OpenChart}" CommandParameter="Pro1"  Content="Pro1" HorizontalAlignment="Left" Margin="240,120,0,0" VerticalAlignment="Top"/>
        <Button Command="{Binding OpenChart}" CommandParameter="Pro2"  Content="Pro2" HorizontalAlignment="Left" Margin="240,157,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

<Window x:Class="Yatp.Desktop.ChartWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Yatp.Desktop"
        xmlns:vm="clr-namespace:Yatp.Desktop;assembly=Yatp.Desktop.Core"
        mc:Ignorable="d"
        d:DataContext="{x:Static vm:Chart.designVm}"
        Title="ChartWindow" Height="450" Width="800">
    <Grid>
        <Label Content="{Binding Product}" HorizontalAlignment="Left" Margin="336,180,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

module Yatp.Desktop.Chart

open Elmish.WPF

type Model = {Product: string}
type Msg = | Dummy

let init = {Product = "Design"}

let bindings() : Binding<Model, Msg> list = [
    "Product" |> Binding.oneWay (fun m -> m.Product)
]

let designVm = ViewModel.designInstance init (bindings())
module Yatp.Desktop.Program

open System
open Serilog
open Elmish.WPF
open Serilog.Extensions.Logging
open System.Windows

//type Model = { Chart_: Chart.Model option }
type Model = { Charts: Map<string, Chart.Model> }

type Msg = 
    | OpenChart of product:string
    | ChartMsg of Chart.Msg

//let init() = { Chart_ = None }
let init() = { Charts = Map.empty }

let update msg m = 
    match msg with
    //| OpenChart product -> { m with Chart_ = Some {Product = product} }
    | OpenChart product -> { m with Charts = m.Charts.Add(product, {Product = product}) }

let bindings (chartBuilder: unit -> #Window) () : Binding<Model, Msg> list = [
    "OpenChart" |> Binding.cmdParam (string >> OpenChart)
    "ChartWindow" |> Binding.subModelWin(
        //(fun m -> WindowState.ofOption m.Chart_),
        (fun m -> WindowState.ofOption (m.Charts |> Map.tryFind "Pro2")),
        snd,
        ChartMsg,
        Chart.bindings,
        chartBuilder)
]

let main mainWindow (chartBuilder: Func<#Window>) =
    let logger =
        LoggerConfiguration()
            .MinimumLevel.Override("Elmish.WPF.Update", Events.LogEventLevel.Verbose)
            .MinimumLevel.Override("Elmish.WPF.Bindings", Events.LogEventLevel.Verbose)
            .MinimumLevel.Override("Elmish.WPF.Performance", Events.LogEventLevel.Verbose)
            .WriteTo.Console()
            .CreateLogger()

    WpfProgram.mkSimple init update (bindings (fun() -> chartBuilder.Invoke()))
    |> WpfProgram.withLogger (new SerilogLoggerFactory(logger))
    |> WpfProgram.startElmishLoop mainWindow

@TysonMN
Copy link
Member

TysonMN commented Nov 11, 2022

If you already have a variable number of buttons, put the SubModelWin binding next to the binding for the button. I think that will work.

@francotiveron
Copy link
Author

Sorry, I cannot figure out how to translate to code what you wrote. Would you please add some code?

@francotiveron
Copy link
Author

@TysonMN
Copy link
Member

TysonMN commented Nov 11, 2022

I asked an answer there

@LyndonGingerich
Copy link
Contributor

@francotiveron It looks as though you've accepted Tyson's answer on Stack Overflow. Is this issue resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants