如何创建动态多色TextBlock?

时间:2017-04-14 02:28:41

标签: c# wpf mvvm

我有一个DataGrid,然后一列是多色TextBlcok

但是此列数据会因其他人的观点而改变。

所以,我认为我需要设计一个ViewModel来绑定它。

现在,我很困惑如何将这个TextBlock创建到我的DataGrid列中。

像:

enter image description here

现在,请致电AB属性,123456是财产日期。

用户将更改属性A的数据,如:

enter image description here

或者,用户将添加属性C的新数据,如:

enter image description here

当然,也许清楚属性B的数据,如:

enter image description here

所有属性都存在于模型中,但未在TextBlock上显示。因此,如果它没有任何数据,它将不显示此列。

顺便说一句,用户需要复制此列的内容,因此我想使用TextBlock进行用户选择。

Run TextBlock绑定差异颜色的单词。

也许这是一个UserControl并添加DataGridTemplateColumn.CellTemplate DataGrid,但是如何绑定和动态创建Run的新TextBlock

也许其他方法我现在不知道。

我觉得有些东西卡在我脑海里。

帮助!

1 个答案:

答案 0 :(得分:1)

我试了一下。它不是一个完整的解决方案,但它可以帮助您,并为您提供解决方案。也许你可以从那里开始。所以,我的代码在这里:

  

型号:

public class DGCollection
    {
        public int ID { get; set; }
        public List<KeyValues> KeyValues { get; set; }
    }

public class KeyValues
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
  

ViewModel:

public class MainWindowViewModel : INotifyPropertyChanged
    {

        public MainWindowViewModel()
        {
            DGCollections = new ObservableCollection<DGCollection>();
            LoadData();
        }

        private ObservableCollection<DGCollection> _DGCollections;

        public ObservableCollection<DGCollection> DGCollections
        {
            get { return _DGCollections; }
            set
            {
                _DGCollections = value;
                NotifyPropertyChanged("DGCollections");
            }
        }

        private void LoadData()
        {
            KeyValues obj1 = new KeyValues { Key = "A", Value = "123" };
            KeyValues obj2 = new KeyValues { Key = "B", Value = "456" };
            KeyValues obj3 = new KeyValues { Key = "C", Value = "789" };
            KeyValues obj4 = new KeyValues { Key = "D", Value = "101112" };
            List<KeyValues> lst = new List<KeyValues>();
            lst.Add(obj1);
            lst.Add(obj2);
            lst.Add(obj3);
            lst.Add(obj4);            
            DGCollections.Add(new DGCollection { ID = 1, KeyValues = lst });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
        }

    }
  

XAML:

<Window.Resources>
    <DataTemplate x:Key="AItemTemplate">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Foreground="Black" Text="{Binding Key}" Grid.Column="0"></TextBlock>
            <TextBlock Foreground="Gray" Text=" : " Grid.Column="1"></TextBlock>
            <TextBlock Foreground="Red" Text="{Binding Value}" Grid.Column="2"></TextBlock>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <DataGrid x:Name="dG" ItemsSource="{Binding DGCollections}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Foreground="Black" Text="{Binding ID}"></TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding KeyValues}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" ItemTemplate="{StaticResource AItemTemplate}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我在这里尝试创建一个DataGridTemplateColumn。在其中使用ItemsControl。并为该ItemsControl分配了一个模板。对于这种方法,您可能必须在模型中进行一些更改,或者必须更改我的示例中的绑定。试一试,如果您需要任何进一步的帮助,请告诉我。