listview组合框同步 [英] listview combobox synchronization
问题描述
大家好,
我将一个组合框与一个外部listview元素与一个DataTable和一个名为"Status"的列进行同步,这是可行的:combobox->列表视图.
现在,我需要将DataTable值"Status"写入组合框,但无法完成此操作:
组合框<-列表视图.
例如,在显示另一个窗口之后,使用组合框返回到窗口"时,这是必需的. ListView始终具有正确设置了状态"的数据表.
我现在有一个问题:如何从隐藏代码"中访问ComboBox"comboBoxStatus"?它应该是可见的,但是在代码完成时我看不到它.另一方面,可以访问ListView"listView1".怎么会呢?
在此先感谢
分类帐
我在XAML中以这种方式将listview与combobox结合在一起:
Hello all,
I synchronize a combobox with an outer listview Element with a DataTable and a column named "Status", which is working: combobox -> listview.
Now I need to write the DataTable value "Status" into the combobox, which I cannot accomplish:
combobox <- listview.
This is needed for example, when returning to the Window with the combobox, after showing a different window. The ListView has always a DataTable with the right "Status" set.
One question I have now is: how can I access ComboBox "comboBoxStatus" from Code Behind? It should be visible, but I cannot see it with code completion. ListView "listView1" on the other hand is accessible. How come?
Thanks in advance
leder
I have combined listview with combobox that way in XAML:
<Grid Background="LightSteelBlue">
<Grid Background="LightSteelBlue">
<ListView Name="listView1" ItemsSource="{Binding}" Background="LightSteelBlue" Visibility="Visible" FontSize="14" IsEnabled="True" Margin="227,67,20,72" IsTextSearchEnabled="True" IsHitTestVisible="True" IsSynchronizedWithCurrentItem="false">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
<GridViewColumn Width="200" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn Width="20" DisplayMemberBinding="{Binding Path=Status}" />
<GridViewColumn Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="comboBoxStatus"
Style="{DynamicResource ButtonlessComboBoxStyle}"
Width="180" Height="Auto" BorderThickness="0" Background="LightSteelBlue"
ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedItem="{Binding Path=OperationState, Mode=TwoWay}"
SelectionChanged="comboBox_SelectionChanged"
HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" IsSynchronizedWithCurrentItem="False" IsReadOnly="False"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=.,Mode=OneWay,
Converter={StaticResource enumItemsConverter}}"
Height="Auto"
Margin="0"
VerticalAlignment="Center"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
<Border Height="61" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Top" Width="191" CornerRadius="0,0,20,0" BorderThickness="1,2" Opacity="2" Padding="0" BorderBrush="Black" Margin="-9,-12,0,0" />
<Label Height="30" HorizontalAlignment="Left" Margin="20,11,0,0" Foreground="SlateGray" FontSize="14" FontStyle="Italic" FontWeight="Bold" Name="label2" VerticalAlignment="Top" Width="212">First Boot Assistent</Label>
<Label Height="29" Margin="208,12,46,0" Foreground="SlateGray" FontSize="14" FontStyle="Italic" FontWeight="Bold" Name="label1" VerticalAlignment="Top">System - Recovery</Label>
<Label Margin="20,66.845,0,72" Name="label3" HorizontalAlignment="Left" Width="184" BorderThickness="1" Background="Transparent" BorderBrush="Bisque">Hier einführender Text ...</Label>
<Button Height="33" HorizontalAlignment="Right" Margin="0,0,20,17" Name="button1" VerticalAlignment="Bottom" Width="70" Click="button1_Click">Start</Button>
<Button Height="33" HorizontalAlignment="Left" Margin="20,0,0,17" Name="button2" VerticalAlignment="Bottom" Width="70" Click="button2_Click">Zurück</Button>
<Button Height="33" Margin="183,0,176,17" Name="button3" VerticalAlignment="Bottom" Click="button3_Click">Konfiguration komplett löschen</Button>
</Grid>
后面的C#代码:
C# Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using FirstBootAssistent.Logic;
using FirstBootAssistent.Utility;
using FirstBootAssistent.ValueConverters;
using FirstBootAssistent.Resources;
namespace FirstBootAssistent.GUI
{
/// <summary>
/// Interaktionslogik für RecoveryConfig.xaml
/// </summary>
public partial class RecoveryConfig : Window
{
private FBAservice fbaService;
public RecoveryConfig(FBAservice service)
{
InitializeComponent();
fbaService = service;
// ListView mit Daten aus der Tabelle füllen
listView1.DataContext = fbaService.Tabele();
//((ComboBox)this.listView1.Items.GetItemAt(0)).ItemsSource = (IEnumerable<int>)fbaService.Tabele();
}
// Starten
private void button1_Click(object sender, RoutedEventArgs e)
{
// Warnung vor der Ausführung anzeigen
Window recoveryWarning = new RecoveryWarning(fbaService);
this.Close();
recoveryWarning.ShowDialog();
}
// Zurück zur Startmaske
private void button2_Click(object sender, RoutedEventArgs e)
{
// Maske für Recovery-Start anzeigen
Window recoveryStart = new RecoveryStart(fbaService);
this.Close();
recoveryStart.ShowDialog();
}
// Konfiguration komplett löschen
private void button3_Click(object sender, RoutedEventArgs e)
{
// Alle Konfigurationsoptionen auf löschen einstellen
if (!fbaService.ResetAllConfigurationData())
{
MessageBox.Show("Einige Daten konten nicht komplett gelöscht werden!", "FBA-Konfiguration",MessageBoxButton.OK, MessageBoxImage.Warning);
}
// Maske für abgeschlossen Prozess anzeigen
Window recoveryFinish = new RecoveryFinish();
this.Close();
recoveryFinish.ShowDialog();
}
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListViewItem))
dep = VisualTreeHelper.GetParent(dep);
if (dep == null)
return;
DataRowView item = (DataRowView)listView1.ItemContainerGenerator.ItemFromContainer(dep);
DataRow dr = fbaService.Tabele().Rows.Find((int)item[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID]);
OperationState myAuswahl = (OperationState)e.AddedItems[0];
Console.Write("Anwendung: " + dr[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID]);
Console.WriteLine("\tStatus: " + myAuswahl);
fbaService.SetConfigData((int)dr[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID], (int)myAuswahl);
listView1.DataContext = fbaService.Tabele();
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
}
推荐答案
简短的答案是:不要使用数据绑定,并且将组合的SelectedIndex属性绑定到随更新而更新的内容上列表框.在您看来是"OperationState"的情况下:无论您在哪里更新名称和状态,也要在OperationState上触发PropertyChanged事件.
该组合在全局范围内不可见,因为它不是一个组合,而是数据模板中的一个组合,因此可以复制.
The short answer is: don''t, use data binding and have the combo''s SelectedIndex property bound to something which gets updated along with the list box. In your case that appears to be ''OperationState'': wherever you are updating Name and Status, also fire a PropertyChanged event on OperationState.
The combo is not visible in global scope because it is not one combo, it is a combo within a data template so it is replicated.
感谢Bob!
我对组合框的XAML所做的唯一更改是:
Thanks Bob!
The only change I made to the XAML of the combobox is:
SelectedIndex="{Binding Path=Status}"
它就像一个魅力...
再见
leder
And it works like a charm...
Bye
leder
这篇关于listview组合框同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!