设置数据网格上堆叠项的可见性

时间:2015-02-17 22:22:39

标签: c# wpf datagrid visibility

我试图在数据网格的堆栈面板中显示文本框或按钮,这是基于WPF数据网格中不同列的下拉列表。以下是基本代码。实际实现将具有每个字段的数据源的绑定值。寻找一种简单优雅的方法,根据下拉列表中的选定值设置文本框和按钮的可见性。任何帮助表示赞赏:

<Grid Name="dg1">
    <DataGrid AutoGenerateColumns="False" Height="200" Name="dataGrid1" Width="500" >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel  Height="30">
                            <TextBox   Name="tbName"  Width="100" />
                            <Button Name="btn1" Content="ADD ME" Width="100" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Choice" Width="150" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  Name="combo1" >
                            <ComboBoxItem Content="ShowButton" />
                            <ComboBoxItem Content="ShowText" />
                        </ComboBox>

                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

我有一个答案...... 经过分析后,第一个回复能够拼凑出以下解决方案

XAML

  <Window.Resources>
    <local:ChoiceToVisibilityConverter x:Key="choice2visibilityConverter" />
    <local:ChoiceList x:Key="ChoiceList" />
</Window.Resources>
 <Grid Name="dg1">
<DataGrid AutoGenerateColumns="False" Height="200" Name="dataGrid1" Width="500" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel  Height="30">
                        <TextBox   Name="tbName"  Width="100" Visibility="{Binding Choice, Converter={StaticResource choice2visibilityConverter},ConverterParameter=TextBox} />
                        <Button Name="btn1" Content="ADD ME" Width="100" Visibility="{Binding Choice, Converter={StaticResource choice2visibilityConverter},ConverterParameter=TextBox} />
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Choice" Width="150" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                        <ComboBox  Name="combo1"  
                                  SelectedValue="{Binding Choice, NotifyOnSourceUpdated=True,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                  ItemsSource="{StaticResource ChoiceList }"
                                   >

                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

C#代码:

 public  class ChoiceToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString().CompareTo("ShowText") == 0)
            return Visibility.Visible ;
        return Visibility.Hidden ;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

public class ChoiceList : List<string>
{
    public ChoiceList()
    {

        this.Add("ShowText");
        this.Add("ShowButton");

    }
}

1 个答案:

答案 0 :(得分:1)

假设您的DataGrid绑定到包含单行信息的模型类的Collection。

您只需添加属性SelectedRow并将其与DataGrid SelectedItem属性绑定即可。在您的列数据模板中,使用值转换器和绑定来显示/隐藏您想要的任何内容。

类似的东西:

<Window.Resources>
            <local:ChoiceToVisibilityConverter x:Key="choice2visibilityConverter" />
    </Window.Resources>

 <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel  Height="30">
                        <TextBox   Name="tbName"  Width="100"  Visibility="{Binding SelectedRow.Choice, Converter={StaticResource choice2visibilityConverter}, ConverterParameter=fromTextBox}"/>
                        <Button Name="btn1" Content="ADD ME" Width="100"  Visibility="{Binding SelectedRow.Choice, Converter={StaticResource choice2visibilityConverter}, ConverterParameter=fromButton}"/>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Choice" Width="150" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox  Name="combo1"  SelectedValue="{Binding SelectedRow.Choice}" SelectedValuePath="Content" >
                        <ComboBoxItem Content="ShowButton" />
                        <ComboBoxItem Content="ShowText" />
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>

所以在这里你可以根据Selected choise和converter参数选择ValueConverter返回的visibilty。