该表达式应该具有类型字符串

时间:2016-06-18 21:43:57

标签: wpf xaml data-binding f# string-concatenation

我正在尝试将5个文本框中的值合并到字符串中以显示在标签中。但是我收到此错误消息

This expression was expected to have type string but here has type string -> string -> string -> string -> string

我可以在标签中显示任何文本框值。 以下是我使用的代码:

let temp = self.Factory.Backing(<@ self.Temp @>, "")
let ph = self.Factory.Backing(<@ self.PH @>, "")
let soil = self.Factory.Backing(<@ self.SOIL @>, "")
let slope = self.Factory.Backing(<@ self.SLOPE @>, "")
let aspect = self.Factory.Backing(<@ self.ASPECT @>, "")

let display = self.Factory.Backing(<@ self.Display @>, "")

let goCommand = self.Factory.CommandSyncParam(fun param -> self.Display <- sprintf "The data that has been enter is:\n%s,\n %s,\n %s,\n %s,\n %s" param)

member x.TEMP with get() = temp.Value and set value = temp.Value <- value
member x.PH with get() = ph.Value and set value = ph.Value <- value
member x.SOIL with get() = soil.Value and set value = soil.Value <- value
member x.SLOPE with get() = slope.Value and set value = slope.Value <- value
member x.ASPECT with get() = aspect.Value and set value = aspect.Value <-value
member x.Display with get() = display.Value and set value = display.Value <- value
member x.GoCommand = goCommand  

我找到了这段代码How do I concatenate a list of strings in F#?,这就是我想要的输出。所以我用我的值尝试了这段代码。

let strings =["temp.Value"; "ph.Value"; "soil.Value"]

let r = strings |> List.fold (fun r s -> r + s + "\n") ""

printfn "%s"

我得到了这个输出:

temp.Value
ph.Value
soil.Value

那么如何获得每个类别的价值以及如何将其合并到“let goCommand?”

根据显示xaml代码的请求进行编辑

        <TextBox Grid.Row="3" Grid.Column="1" FontSize="14" Text="{Binding TEMP}"/>
    <TextBox Grid.Row="4" Grid.Column="1" FontSize="14" Text="{Binding PH}"/>
    <TextBox Grid.Row="5" Grid.Column="1" FontSize="14" Text="{Binding SOIL}"/>
    <TextBox Grid.Row="6" Grid.Column="1" FontSize="14" Text="{Binding SLOPE}"/>
    <TextBox Grid.Row="7" Grid.Column="1" FontSize="14" Text="{Binding ASPECT}" Grid.ColumnSpan="3" Margin="0,0,16,0" />
    <Button Margin="0,31,0,652" Grid.Row="8" FontSize="16" Content="Submit" Foreground="DarkBlue" FontWeight="Heavy" Background="LightBlue"
        Command="{Binding GoCommand}" CommandParameter="{Binding TEMP, PH, SOIL,SLOPE, ASPECT}"/>

1 个答案:

答案 0 :(得分:0)

您的属性(x.TEMP,...)可从MainViewModel获得 - 您不需要尝试将其值作为参数传输。因此,您需要使用不带任何参数的命令:

的.xaml:

    <Button Margin="0,31,0,652" Grid.Row="8" FontSize="16"
     Content="Submit" Foreground="DarkBlue" FontWeight="Heavy" 
Background="LightBlue" Command="{Binding GoCommand}" />

.FS:

let display = self.Factory.Backing(<@ self.Display @>, "")

let go() = 
    self.Display <- sprintf "The data that has been enter is:\n%s,\n %s,\n %s,\n %s,\n %s" 
        self.TEMP self.PH self.SOIL self.SLOPE self.ASPECT

let goCommand = self.Factory.CommandSync(go)

修改

完整.fs:

type MainViewModel() as self = 
    inherit ViewModelBase()    

    let temp = self.Factory.Backing(<@ self.TEMP @>, "")
    let ph = self.Factory.Backing(<@ self.PH @>, "")
    let soil = self.Factory.Backing(<@ self.SOIL @>, "")
    let slope = self.Factory.Backing(<@ self.SLOPE @>, "")
    let aspect = self.Factory.Backing(<@ self.ASPECT @>, "")

    let display = self.Factory.Backing(<@ self.Display @>, "")

    let go() = 
        self.Display <- sprintf "The data that has been enter is:\n%s,\n %s,\n %s,\n %s,\n %s" 
            self.TEMP self.PH self.SOIL self.SLOPE self.ASPECT

    let goCommand = self.Factory.CommandSync(go)

    member x.TEMP with get() = temp.Value and set value = temp.Value <- value
    member x.PH with get() = ph.Value and set value = ph.Value <- value
    member x.SOIL with get() = soil.Value and set value = soil.Value <- value
    member x.SLOPE with get() = slope.Value and set value = slope.Value <- value
    member x.ASPECT with get() = aspect.Value and set value = aspect.Value <-value
    member x.Display with get() = display.Value and set value = display.Value <- value
    member x.GoCommand = goCommand