C#WPF根据其ItemsSource

时间:2018-08-28 17:26:38

标签: c# wpf datagrid

如何根据列中的项目是否具有设置为ItemsSource的{​​{1}}属性,使组合框 可编辑该列正在生成?

2 个答案:

答案 0 :(得分:1)

只需在选定的索引更改事件或主窗口加载时选中组合框itemsource,然后为该选定的项目设置组合框var fn = function() { return 1917; }; fn.oldToString = fn.toString; fn.toString = function() { return "That fine function I wrote recently: " + this.oldToString(); }; var that = fn; console.log(that);

答案 1 :(得分:1)

您可以使用ComboBoxDataTrigger添加样式,以检查ItemsSource是否为空。

这是一个非常简单的示例:

<Window x:Class="Q52063400.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Title="MainWindow" Height="450" Width="800">
  <Grid>
      <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
          <DataGrid.Columns>
              <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
              <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" />
              <DataGridTemplateColumn Header="City">
                  <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                          <ComboBox ItemsSource="{Binding Cities}" 
                                    SelectedItem="{Binding SelectedCity, Mode=TwoWay}">
                              <ComboBox.Style>
                                  <Style TargetType="ComboBox">
                                      <Setter Property="IsEditable" Value="False"/>
                                      <Style.Triggers>
                                          <DataTrigger Binding="{Binding Cities}" Value="{x:Null}">
                                              <Setter Property="IsEditable" Value="True"/>
                                          </DataTrigger>
                                      </Style.Triggers>
                                  </Style>
                              </ComboBox.Style>
                          </ComboBox>
                      </DataTemplate>
                  </DataGridTemplateColumn.CellTemplate>
              </DataGridTemplateColumn>
          </DataGrid.Columns>
      </DataGrid>
  </Grid>
</Window>

隐藏代码:

public partial class MainWindow : Window
{
   public MainWindow()
   {
       InitializeComponent();

       DataContext = GetItems();
   }

   public List<Person> GetItems ()
   {
       return new List<Person>()
       {
           new Person {Name = "John", Surname = "Rocky", Cities = new List<string> { "New York City", "Washington D.C.", "Chicago" } },
           new Person {Name = "John", Surname = "Rocky"},
           new Person {Name = "John", Surname = "Rocky", Cities = new List<string> { "Baltimore", "San Francisco", "Boston" } }
       };
   }
}

public class Person
{
   public string Name { get; set; }
   public string Surname { get; set; }

   public IEnumerable<string> Cities { get; set; }
   public string SelectedCity { get; set; }
}

基于ItemsSource使用不同编辑器的示例:

...
<DataGridTemplateColumn Header="City">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>                           
            <Grid>
                <TextBox Text="{Binding SelectedCity, Mode=TwoWay}">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Cities}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <ComboBox ItemsSource="{Binding Cities}" 
                      SelectedItem="{Binding SelectedCity, Mode=TwoWay}">
                    <ComboBox.Style>
                        <Style TargetType="ComboBox">
                            <Setter Property="Visibility" Value="Visible"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Cities}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Style>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...