简体   繁体   English

如何在WPF绑定中将UIElement或XAML作为内容返回

[英]How to return UIElement or XAML as content in WPF Binding

Hi and excuse me for my bad english. 嗨,请原谅我的英语不好。

Well... I'm trying to bind to a list of objects to fill my ItemsControl. 好吧...我试图绑定到对象列表以填充我的ItemsControl。 Object represents a pair of name and value of settings taken from config file. 对象表示从配置文件中获取的一对名称和设置值。 The purpose is to make some settings editor which saves edited data back to file. 目的是制作一些设置编辑器,将编辑的数据保存回文件。

Object is quite simple: 对象很简单:

MySettings
{
public string Name { get; set; }
public string Value { get; set; }
}

There is some simple markup (this is a sketch, not exact project): 有一些简单的标记(这是一个草图,而不是确切的项目):

<ItemsControl Name="myItmCtrl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <!--Name of the property-->
                <TextBlock Text="{Binding Name}" />
                <!--Its value-->
                <TextBox Text="{Binding Value}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And at the end i'm adding myItmCtrl.ItemsSource = List<MySettings>; 最后,我要添加myItmCtrl.ItemsSource = List<MySettings>; in code-behind. 在代码背后。

It all works fine as it shoud. 一切都应正常工作。

Then I desided to advance my form because some of the Value s (which are all stored as strings) can take values of "TRUE" and "FALSE" which can be easily converted to bool . 然后我desided推进我的形式,因为一些的Value S(其全部存储为字符串)可以采取的值"TRUE""FALSE" ,其可以容易地转化为bool So, instead of typing TRUE or FALSE in some cases, I want to represent those values by kinda different UIElement - CheckBox - and then bind to its IsChecked property. 因此,我不想在某些情况下键入TRUE或FALSE,而是想通过不同的UIElement- CheckBox来表示这些值,然后绑定到其IsChecked属性。

So, I want that string values were represented by TextBox es and bool values by CheckBox es. 因此,我希望字符串值由TextBox表示,布尔值由CheckBox I'd could do it in code-behind and create elements on the fly in loop through List<MySettings> but i didn't. 我可以在后台代码中做到这一点,并通过List<MySettings>创建元素,但我没有。 The idea is to find some tricky extension which can describe all of it (or some 50/50 xaml/code-behind) in XAML. 这样做的目的是找到一些棘手的扩展名,以描述XAML中的所有扩展名(或50/50 xaml /后台代码)。

Many thanks 非常感谢

The best solution would be using a ValueConverter that converts between string and bool values. 最好的解决方案是使用在字符串和布尔值之间转换的ValueConverter Since we already got some decent methods for how to convert strings to booleans and vice versa, this shouldn't be a problem. 由于我们已经有了一些不错的方法来将字符串转换为布尔值,反之亦然,所以这应该不是问题。 You can build your own ValueConverter just as the article mentioned by amnezjak describes. 您可以构建自己的ValueConverter就像amnezjak提到的文章所述。

The binding for your TextBox may be as follows: 您的TextBox的绑定可能如下:

<TextBox Text="{Binding Value, Converter={StaticResource myConverter}}"/>

myConverter being the key to the actual converter you've imported. myConverter是您导入的实际转换器的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM