使用数据绑定处理样式 [英] Handling styling with data binding

查看:46
本文介绍了使用数据绑定处理样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重写

我有一个接收文件的应用程序。该文件具有大量可编辑的内容。此内容有多种可能的类型(即布尔复选框,文本框等)。问题在于,这些值可以单独使用,也可以成组使用(最多8个),因此它们以数组形式出现。我们将这些值绑定到 ListView ,并使用 DataTemplates 显示它们。实际上,我从数组列表中创建了 ListView

I have an application that receives a file. This file has a large amount of editable content. This content comes in a variety of possible types (i.e. boolean checkboxes, textboxes, etc). The issue is, these values can be either alone, or in a group (up to 8), so they come in arrays. We bind these values to a ListView, and use DataTemplates to display them. Effectively, I create the ListView from a list of arrays.

这些数组中的项目需要进行数据绑定并设置正确的样式(例如,布尔数组需要创建复选框,而字符串数组需要文本框)。每个创建的元素都需要放在 ListView 的一列中。当前样式是将 DataTemplates 与数据绑定一起使用,即

The items in these arrays need to be data bound and styled properly (for example, a boolean array needs to create checkboxes, while a string array needs textboxes). Each created element needs to be put into a column in the ListView. The current styling is using DataTemplates with data binding, i.e.

<DataTemplate x:Key="testTemplate2">
    <TextBlock Text="{Binding Path=Value[0]}" 
               Margin="2" 
               HorizontalAlignment="Center" 
               VerticalAlignment="Center" />
</DataTemplate>

对输入数组中的每个值重复此操作,因此您有 Value [1] Value [2] 等。

This is repeated for every value in the input array, so you have Value[1], Value[2], etc.

这意味着几乎重复相同的代码8次,然后对下一个类型执行相同的操作。由于存在大量的输入类型,这意味着大量重复代码。

This means repeating almost the same code 8 times, and then doing the same for the next type. Since there is a large amount of input types, this means a ridiculous amount of repeated code.

我的问题是:是否有更好的方法来做到这一点,

顺便说一句,我使用的是.NET 3.5。

By the way, I am using .NET 3.5.

行的外观示例。每个元素将在其自己的列中。组合框是根据数组构建的。

Example of how a row would look like. Each element would be in its own column. The comboboxes are built from the array.

EDIT
示例DataTemplate:

EDIT Example DataTemplate:

<DataTemplate x:Key="textBoxTemplate2">
    <TextBox Text="{Binding Path=Value[2], NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
                     BorderBrush="{DynamicResource ComboBorder}"
                     Tag="{Binding Path=AllowedChars}"
                     PreviewTextInput="PreviewTextInputHandler"
                     DataObject.Pasting="PastingHandler"
                     ToolTip="{Binding Path=Title}" 
                     Margin="2" 
                     SourceUpdated="BindingSourceUpdated"
                     MaxLength="{Binding Path=DataLength}"
                     HorizontalAlignment="Stretch" 
                     VerticalAlignment="Center" >
        <TextBox.IsEnabled>
            <MultiBinding Converter="{StaticResource isEnabledConverter}">
                <Binding Path="IsLocked" Mode="OneWay" />
                <Binding Path="IsVisible[2]" Mode="OneWay" />
            </MultiBinding>
        </TextBox.IsEnabled>
    </TextBox>
</DataTemplate>

示例图:

我有一个ViewModel。此ViewModel具有一个由ItemData组成的列表。 ItemData类具有一个称为值的数组。该列表绑定到视图。我们需要根据要访问的ItemData的属性来选择要使用的DataTemplate:

I have a ViewModel. This ViewModel has a List, made of ItemData. Class ItemData has an array called Values. The List is bound to the View. We need to choose what DataTemplate to use depending on what property of ItemData we're accessing:


  1. 一个名称

  2. 一个或多个Option arrray。

当前,我们在ListView中显示列表。生成 ListView 时,各列的 CellTemplate DataTemplates $ c>,每个索引一个,总共8个DataTemplates。

Currently, we display the List in a ListView. When generating the ListView, the columns have different DataTemplates attached to their CellTemplates, one per index, for a total of 8 DataTemplates.

推荐答案

我的答案集中在您的单词上:由于输入类型很多,这意味着重复代码非常荒谬。

My Answer is focused on your words : Since there is a large amount of input types, this means a ridiculous amount of repeated code.

代码重用:

Code Reuse:

由于您在 Item模板中需要为不同的控件定义不同类型的控件 DataTypes ,因此无法完全减少代码。我的意思是,如果您要 TextBox 作为 String 类型或 Checkbox 对于 Bool 类型,无法明显减少代码。但是,您可以减少的是为不同的模板一次又一次地定义 Binding 语法,正如您在<$ c中看到的那样$ c>文本框 模板示例。您可以定义 Biniding 一次,然后一次使用n个(在您的情况下为8个)控件重复使用它们。以下是您的操作方法:

Since you in your Item template needs to define different kind of controls for different DataTypes, so that code can't be reduced completely. I mean if you want TextBox for String type or Checkbox for Bool type that code can't be reduced obviously. However what you can reduce is defining Binding syntax again and again for different template as I can see in your TextBox Template example. You can define the Biniding once and then reused them again and again with n number(8 in your case) of controls. Below is how you do it:

public class BindingResourceExtension : StaticResourceExtension
{
    public BindingResourceExtension() : base() { }

    public BindingResourceExtension(object resourceKey) : base(resourceKey) { }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var binding = base.ProvideValue(serviceProvider) as BindingBase;
        if (binding != null)
            return binding.ProvideValue(serviceProvider);
        else
            return null; //or throw an exception
    }
}

XAML

XAML

<Window.Resources>
    <ResourceDictionary>
        <Binding x:Key="MyBinding" Path="MyProperty" Mode="TwoWay" />
    </ResourceDictionary>
</Window.Resources>

(...)

<TextBox Text="{ns:BindingResource MyBinding}" />
<CheckBox IsChecked="{ns:BindingResource MyBinding}" />

因此可以实现一些代码重用(将上面的代码成像为大型而复杂的绑定)。在您发布问题之后,我正在搜索类似内容,因此我发布了另一个绑定重用问题它有所帮助。同样,由于绑定将集中化,因此易于更新。

So some reuse of code can be achieved( Imaging above code with large and complex bindings). After you posted your question I was searching for something like this so I posted another question for binding reuse and it helped. Also as Bindings will be centralize they will be easy to update.

ItemTemplate:

ItemTemplate:

除了代码重用问题之外,您还可以使用嵌套的 ItemsControl 我可以看到并在另一个答案中建议该类语法:

Apart from your code reuse problem you can use nested ItemsControl as by looking your class digram I can see and also suggested in another answer:

<ListBox ItemsSource="{Binding CollectionOfArrays}">
<ListBox.ItemTemplate>
    <DataTemplate>
        <ItemsControl ItemsSource="{Binding Array}" />
    </DataTemplate>
</ListBox.ItemTemplate>

现在内部 ItemsControl ,您必须实际定义模板,但我认为您已经很清楚了。

Now for inner ItemsControl you have to actually define the Templates, but I think you are already clear on that part.

这篇关于使用数据绑定处理样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆