2015-09-06 32 views
1

我很新来WPF(从WinForms移动)。我试图将一些场景从WinForms应用程序转移到WPF之一:WPF - 如何为特定场景绑定正确的方式?

  1. 一个窗口有一个带有3列的ListView控件。
  2. 有一个按钮可以将新行添加到该ListView。
  3. 第一列和第二列包含ComboBox控件。
  4. 第三列必须包含不同的控件,但一次只能显示一个。哪一个是可见的,它取决于第一列中ComboBox的选定值。
  5. 每当用户从第一列的ComboBox中选择一个值时,第二列ComboBox的内容就会更改。

一般的情形是:用户选择从从第一组合框类型列表中的一个类型,该第二组合框改变其内容,以所支持的操作的列表中选择的类型,并在该第三列后时间必须更改其内容以显示支持该类型输入的控件。

我知道如何使用WinForms来实现它,但我不知道如何使用WPF来实现它。有人可以帮助我实现它,或任何人都可以帮助实现这一目标的信息吗?

我到目前为止的代码:

public class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
    } 

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) 
    { 
     if (PropertyChanged != null) PropertyChanged(this, args); 
    } 
} 

public class RecordFilter : ViewModelBase 
{ 
    private static readonly ObservableCollection<KeyValuePair<PropertyInfo, string>> ColumnAliases = 
     new ObservableCollection<KeyValuePair<PropertyInfo, string>>(Card.ColumnAliases); 

    private KeyValuePair<PropertyInfo, string> _currentSelectedProperty; 

    public IEnumerable<OperationInfo> Operations 
    { 
     get 
     { 
      return Operations.GetOperationInfosForType(GetTypeUnwrapNullable(SelectedProperty.Key.PropertyType)); 
     } 
    } 

    public OperationInfo SelectedOperation { get; set; } 

    public KeyValuePair<PropertyInfo, string> SelectedProperty 
    { 
     get { return _currentSelectedProperty; } 
     set 
     { 
      _currentSelectedProperty = value; 

      OnPropertyChanged("Operations"); 
     } 
    } 

    public ObservableCollection<KeyValuePair<PropertyInfo, string>> Properties 
    { 
     get { return ColumnAliases; } 
    } 

    //DateTime or int or float, depends on the selected property type 
    //public object PropertyValue { get; set; } 
} 

这里是XAML代码:

<Window 
 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 
     xmlns:Converters="clr-namespace:App.Converters" x:Class="App.DialogWindows.CardFilterWindow" 
 
     Title="Search filters" Height="347" Width="628" x:Name="wdw" ShowInTaskbar="False" WindowStartupLocation="CenterScreen"> 
 
    <Window.Resources> 
 
     <Converters:NotNullObjectToEnabledConverter x:Key="NotNullObjectToEnabledConverter"/> 
 
    </Window.Resources> 
 
    <DockPanel> 
 
     <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center" Height="Auto"> 
 
      <Button x:Name="bnOK" Margin="5" Width="41" Content="OK" IsDefault="True" Click="bnOK_Click"/> 
 
      <Button x:Name="bnCancel" Margin="5" Content="Отмена" IsCancel="True"/> 
 
     </StackPanel> 
 
     <ListView ItemsSource="{Binding Filters, ElementName=wdw}" Name="LvExpr" DataContext="{Binding Filters, ElementName=wdw}"> 
 
      <ListView.Resources> 
 
       <Style TargetType="{x:Type ListViewItem}"> 
 
        <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
 
       </Style> 
 
      </ListView.Resources> 
 
      <ListView.View> 
 
       <GridView> 
 
        <GridViewColumn Header="Alias" Width="210"> 
 
         <GridViewColumn.CellTemplate> 
 
          <DataTemplate> 
 
           <ComboBox VerticalAlignment="Center" 
 
            ItemsSource="{Binding Properties}" 
 
            DisplayMemberPath="Value" 
 
            SelectedValue="{Binding SelectedProperty, Mode=TwoWay}" 
 
            /> 
 
          </DataTemplate> 
 
         </GridViewColumn.CellTemplate> 
 
        </GridViewColumn> 
 
        <GridViewColumn Header="Operation" Width="150"> 
 
         <GridViewColumn.CellTemplate> 
 
          <DataTemplate> 
 
           <ComboBox VerticalAlignment="Center" 
 
            ItemsSource="{Binding Operations}" 
 
            DisplayMemberPath="OperationAlias" 
 
            SelectedValue="{Binding SelectedOperation, Mode=TwoWay}" 
 
           /> 
 
          </DataTemplate> 
 
         </GridViewColumn.CellTemplate> 
 
        </GridViewColumn> 
 
        <GridViewColumn Header="Value" Width="100"> 
 
         <GridViewColumn.CellTemplate> 
 
          <DataTemplate> 
 
           <TextBox Text="ValidatesOnDataErrors=True}" /> 
 
          </DataTemplate> 
 
         </GridViewColumn.CellTemplate> 
 
        </GridViewColumn> 
 
        <GridViewColumn Width="33"> 
 
         <GridViewColumn.CellTemplate> 
 
          <DataTemplate> 
 
           <Button Tag="{Binding Mode=OneWay}" Click="BnDelete_Click" ToolTip="Delete filter"> 
 
            <Image Source="delete.ico" Height="16" Width="16"/> 
 
           </Button> 
 
          </DataTemplate> 
 
         </GridViewColumn.CellTemplate> 
 
         <GridViewColumnHeader> 
 
          <DataGridCell> 
 
           <Button Click="ButtonAdd_Click" Height="22" Padding="0" ToolTip="Add filter"> 
 
            <Image Source="plus.ico" Focusable="False"/> 
 
           </Button> 
 
          </DataGridCell> 
 
         </GridViewColumnHeader> 
 
        </GridViewColumn> 
 
       </GridView> 
 
      </ListView.View> 
 
     </ListView> 
 
    </DockPanel> 
 
</Window>

+0

到目前为止请显示你的进度,否则你很可能会让你的问题关闭。此外,WPF是关于DataBinding的。这个想法是,你需要一个ViewModel来完成逻辑,而不是代码背后的UI或其他。 –

+0

实际上,我设法实现了我所描述的几乎所有内容,除了在第三列显示适当的控制外。我不知道如何显示字符串类型的TextEdit控件(当用户在ComboBox中选择该类型时)或显示DateTime类型的DateTimePicker控件... –

+2

@ArlenKeshabyan请发布相关代码和XAML。 –

回答

1

在您的视图模型,建立了列表属性,并把它们过滤掉因此当选定的值发生变化时(通过INotifyPropertyChanged.PropertyChanged事件)。

查看this后的一个全面的例子。它使用了一种名为MVVM的技术,广泛用于WPF并代表ModelViewViewModel。我强烈建议你学习这一技术,并将其用于与XAML相关的项目。 Here是一个快速入门教程,在众多的网络中。

+0

感谢您的链接!它们非常有用,但我已经使用ViewModel和适当的绑定实现了级联ComboBoxes。剩下的唯一一件事就是在第三栏显示不同类型的控件...... –

相关问题