Question WPF-Listview template

Plus d'informations
il y a 3 ans 3 mois #23334 par marcci
WPF-Listview template a été créé par marcci
Bonjour a tous,
je continue mon exploration de WPF.

J'ai une listview avec des colonnes 'templaté' avec des textbox lié à un objet datatable.
Jusque la tout va bien.

Je souhaiterais pouvoir agir sur les events des textbox.
Malheureusement je ne sais pas comment les atteindre.

Voici un extrait de mon XAML.

[code:1]
<ListView Name=\"Lstv_CheckPointResources\" HorizontalAlignment=\"Left\" Height=\"336\" VerticalAlignment=\"Top\" Width=\"788\" SelectionMode=\"Extended\" Margin=\"0,10,0,0\" HorizontalContentAlignment=\"Center\">
<ListView.View>
<GridView>
<GridViewColumn Header=\"\" Width=\"22\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Ellipse Fill=\"DarkGreen\" Stroke=\"Black\" Height=\"10\" Width=\"10\" HorizontalAlignment=\"Center\" VerticalAlignment=\"Center\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=\"Server\" Width=\"300\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding 'Server'}\" Width=\"300\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=\"Lan\" Width=\"160\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding 'Lan'}\" Width=\"160\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=\"Nat\" Width=\"160\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding 'Nat'}\" Width=\"160\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=\"Port\" Width=\"70\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding 'Port'}\" Width=\"70\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header=\"Protocol\" Width=\"70\">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding 'Protocol'}\" Width=\"70\"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
[/code:1]

La listview s'affiche correctement les textbox se remplisse avec la datatable.
Mais je ne peut pas dans mon code interagir avec les textbox.

Si besoin je poserais un code fonctionel.<br><br>Message édité par: marcci, à: 17/03/17 16:40

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 11 mois #23953 par marcci
Réponse de marcci sur le sujet Re:WPF-Listview template
Bonjour,
après des heures de recherches et de bidouillage, j'ai enfin trouvé une solution à mon problème.
C'est laid, et aussi as been que Windows.form

Mais ça marche :woohoo:

source: http://www.c-sharpcorner.com/UploadFile/87b416/dynamically-adding-or-retrieves-templates-in-to-controls-in/

[code:1]
[xml]$xaml = @\&quot;
&lt;Window
xmlns=\&quot; schemas.microsoft.com/winfx/2006/xaml/presentation\" ;
xmlns:«»x=\&quot; schemas.microsoft.com/winfx/2006/xaml\" ;
x:Name=\&quot;Window\&quot; Title=\&quot;Initial Window\&quot; WindowStartupLocation = \&quot;CenterScreen\&quot;
Width = \&quot;500\&quot; Height = \&quot;400\&quot; ShowInTaskbar = \&quot;True\&quot;&gt;
&lt;Grid x:Name=\&quot;GeneralGrid\&quot;/&gt;
&lt;/Window&gt;
\&quot;@
[/code:1]
[code:1]
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$windows = [windows.markup.Xamlreader]::Load($reader)
$GeneralGrid = $windows.FindName('GeneralGrid')

$colDefin = 'Hello','ButtonNember','Upper'
$Resources = New-Object System.Data.DataTable
foreach($col in $colDefin){
[void]$Resources.Columns.Add($col)
}


[System.Windows.Controls.TextChangedEventHandler]$ToUpper =
{
$Event = $_
$Resources.rows[[int32]($Event.Source.Name.Substring(1))].Upper = $Event.Source.Text.ToUpper()
}

$GridView = New-Object System.Windows.Controls.GridView

$GridViewColumn = New-Object System.Windows.Controls.GridViewColumn
$GridViewColumn.Header = 'Bouton'
$GridViewColumn.Width = 50
$DataTemplate = New-Object System.Windows.DataTemplate
$button = new-object System.Windows.FrameworkElementFactory([System.Windows.Controls.Button])
$BindingNumber = New-Object System.Windows.Data.Binding(\&quot;ButtonNember\&quot;«»)
$button.SetBinding([System.Windows.Controls.Button]::NameProperty,$BindingNumber)
$button.SetValue([System.Windows.Controls.Button]::ContentProperty,'OK')
$DataTemplate.VisualTree = $button
$GridViewColumn.CellTemplate = $DataTemplate
$GridView.Columns.Add($GridViewColumn)

$GridViewColumn2 = New-Object System.Windows.Controls.GridViewColumn
$GridViewColumn2.Header = 'Data'
$GridViewColumn2.Width = 170
$DataTemplate2 = New-Object System.Windows.DataTemplate
$TextBox = new-object System.Windows.FrameworkElementFactory([System.Windows.Controls.TextBox])
$Binding = New-Object System.Windows.Data.Binding(\&quot;Hello\&quot;«»)
$BindingName = New-Object System.Windows.Data.Binding(\&quot;ButtonNember\&quot;«»)
$TextBox.SetBinding([System.Windows.Controls.TextBox]::TextProperty,$Binding)
$TextBox.SetBinding([System.Windows.Controls.TextBox]::NameProperty,$BindingName)
$TextBox.SetValue([System.Windows.Controls.TextBox]::WidthProperty,150.00)
$TextBox.AddHandler([System.Windows.Controls.TextBox]::TextChangedEvent,$ToUpper,$true)
$DataTemplate2.VisualTree = $TextBox
$GridViewColumn2.CellTemplate = $DataTemplate2
$GridView.Columns.Add($GridViewColumn2)

$GridViewColumn3 = New-Object System.Windows.Controls.GridViewColumn
$GridViewColumn3.Header = 'ToUpper'
$GridViewColumn3.Width = 170
$DataTemplate3 = New-Object System.Windows.DataTemplate
$TextBox1 = new-object System.Windows.FrameworkElementFactory([System.Windows.Controls.TextBox])
$BindingUP = New-Object System.Windows.Data.Binding(\&quot;Upper\&quot;«»)
$TextBox1.SetBinding([System.Windows.Controls.TextBox]::TextProperty,$BindingUP)
$TextBox1.SetValue([System.Windows.Controls.TextBox]::WidthProperty,150.00)
$DataTemplate3.VisualTree = $TextBox1
$GridViewColumn3.CellTemplate = $DataTemplate3
$GridView.Columns.Add($GridViewColumn3)

$LV = New-Object System.Windows.Controls.ListView
$LV.Height = 300
$LV.Width = 475
$LV.Margin = New-Object System.Windows.Thickness(0,-50,0,0)
$LV.view = $GridView
$LV.ItemsSource = $Resources.DefaultView

$GeneralGrid.Children.Add($LV)

[void]$Resources.rows.Add('yes','n0',\&quot;\&quot;«»)
[void]$Resources.Rows.Add('no','n1',\&quot;\&quot;«»)
[void]$Resources.Rows.Add('Bonjour','n2',\&quot;\&quot;«»)
[void]$Resources.Rows.Add('Hello','n3',\&quot;\&quot;«»)

$windows.showdialog()|Out-Null
[/code:1]<br><br>Message édité par: marcci, à: 14/07/17 11:04

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.184 secondes
Propulsé par Kunena