2015-05-11 2 views
0

Я хочу создать простой вид свойств, в котором можно изменить каждое значение.Определить автоматически сгенерированный TextBox

Свойства сгруппированы по одному имени, как это:

  • name1: property1, свойство2
  • name2: property1, свойство2
  • ...

S о я создал DataGrid с шаблоном для заполнения сетки (обратите внимание, что я удалил все свойства стиля и т.д., а текстовые значения также являются только примерами):

<DataGrid Name="propertyGrid"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Property Group Name"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding propertyGroupName}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTemplateColumn Header="Property 1"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox Text="{Binding property1}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTemplateColumn Header="Property 2"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBox Text="{Binding property2}" TextChanged="TextBox_TextChanged" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

Как вы можете видеть, что я сейчас пытаюсь добавить a TextChanged, и есть моя проблема: откуда я могу получить информацию propertyGroupName, так как мне нужно только изменить property2 от определенного propertyGroup.

Я готов для любой подсказку или решение ... может быть, «auto gen datagrid» - не лучшее решение здесь?

Редактировать Мой код позади. Здесь вы можете увидеть страницу, заполняющий DataGrid и класс я связывающийся с (обратите внимание, что метод GetPropertyX просто читает мой файл свойств):

public PropertiesPage() 
    { 
     InitializeComponent(); 

     List<PropertyGroup> properties = new List<PropertyGroup>(); 
     properties.Add(GetPropertyGroup("propertyGroup1")); 
     properties.Add(GetPropertyGroup("propertyGroup2")); 
     properties.Add(GetPropertyGroup("propertyGroup3")); 

     propertyGrid.ItemsSource = properties; 

    } 

    private PropertyGroup GetPropertyGroup(string propertyGroupName) 
    { 
     return new CarrierConfig() 
     { 
      PropertyGroupName = propertyGroupName, 
      Property1 = GetProperty1(propertyGroupName), 
      Property2 = GetProperty2(propertyGroupName) 
     }; 
    } 

    public class PropertyGroup 
    { 
     public string PropertyGroupName { get; set; } 
     public string Property1 { get; set; } 
     public string Property2 { get; set; } 
    } 
+0

вы ищете сетку недвижимости? если да, посмотрите [здесь] (http://wpg.codeplex.com/) – Muds

+0

Нет надежного способа сделать это с помощью DataGrid. Если вы публикуете класс, на который вы привязаны, я могу вам помочь. Возможно, TreeView с HierarchicalDataTemplate будет хорошим выбором. – Domysee

+0

@Muds спасибо, я посмотрю на это! – Sonnywhite

ответ

1

Вы можете связать PropertyGroup с TextBox в Tag, то вы может прочитать его в обработчик событий и проверьте имя группы недвижимости:

<DataGrid Name="propertyGrid"> 
<DataGrid.Columns> 
    <DataGridTemplateColumn Header="Property Group Name"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding propertyGroupName}" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn Header="Property 1"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding property1}" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
    <DataGridTemplateColumn Header="Property 2"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding property2}" Tag="{Binding Path=.}" TextChanged="TextBox_TextChanged" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 

Единственным отличием является Tag="{Binding Path=.}".

обработчик события:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     var textbox = (sender as TextBox); 
     if ((textbox.Tag as PropertyGroup).PropertyGroupName == "the name you want") 
     { 
      //do stuff 
     } 
    } 
+0

Это работает для меня, но выглядит немного рискованно ... но так как я новичок в C# и WPF, я не могу оценить это решение :) ... Я буду использовать его! Благодаря ;) – Sonnywhite

Смежные вопросы