如何使用StringFormat绑定

时间:2015-08-13 08:56:11

标签: wpf xaml

我有ComboBox我绑定了IEnumerable<int>来源。

源代码的值为12,13,14,但我希望ComboBox显示版本12,版本13,版本14等,SelectedValue仍为12,13和14。

现在我正在修改Source以向其添加Version,然后将ComboBox绑定到IEnumerable。

XAML

    <ComboBox x:Name="ComboBoxVersions"  
              SelectedIndex="0" 
              SelectionChanged="ComboBoxVersions_OnSelectionChanged" 
              ItemsSource="{Binding EnvironmentVersions}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

4 个答案:

答案 0 :(得分:4)

你可以使用这样的东西

 <TextBlock Text="{Binding StringFormat=Version: {0}}" />

答案 1 :(得分:4)

使用ComboBox.ItemStringFormat:

import UIKit

var data = [16, 97, 13, 55, 95, 53, 18, 10, 79, 53, 79, 34, 50, 34, 0, 91, 94, 55, 6, 38, 7]

func exchange<T>(inout data:[T], i: Int, j: Int) {
    let temp = data[i]
    data[i] = data[j]
    data[j] = temp
}

func swapLeft<T: Comparable>(inout data: [T], index: Int) {
    for i in reverse(1...index) {
        if data[i] < data[i-1] {
            exchange(&data, i, i-1)
        }else {
            break
        }
    }
}

func isort<T: Comparable>(inout data: [T]) {
    for i in 1...data.count {
        swapLeft(&data,i)
    }
}
data //result [16, 97, 13, 55, 95, 53, 18, 10, 79, 53, 79, 34, 50, 34, 0, 91, 94, 55, 6, 38, 7]
isort(&data)
data //no result shown

或使用ComboBox.ItemTemplate

APIManager.sharedInstance.approveApprovalRequest(notificatio

<ComboBox ItemsSource="{Binding EnvironmentVersions}" 
          ItemStringFormat="version: {0}" />

答案 2 :(得分:2)

这是一个简单的方法:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Version " />
        <TextBlock Text="{Binding}" />
    </StackPanel>
</DataTemplate>

由于ItemTemplate仅定义了项目的显示方式,因此SelectedItem的{​​{1}}属性仍保留着您的版本号集合中的原始值。

答案 3 :(得分:0)

您可以在datatemplate中为绑定添加字符串格式。

@WebService
public class SOAPService{

        private final static AtomicInteger id = new AtomicInteger();
        private WebServiceContext webServiceContext; 

        public SOAPService(WebServiceContext webServiceContext){
                this.webServiceContext = webServiceContext;
        }

        @Override
        public int startUpload(@WebParam String name) {
            String username = Authentication.getUsername(webServiceContext.getMessageContext());

            try {
                File file = new File(username + "/" + name);
                file.createNewFile();
            } catch (IOException e) {
                LOG.error(e.getMessage(), e);
                e.printStackTrace();
            }
            return id.getAndIncrement();
        }
}

java.lang.NullPointerException
    at org.example.SOAPService.startUpload(SOAPService.java:97)
    at org.example.services.SOAPServiceTest.startUploadFileTest(SOAPServiceTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)