在Wpf GridView中如何将控件绑定到一个Item

时间:2016-12-01 08:06:55

标签: c# wpf gridview binding

我想根据复选框隐藏/显示项目中的TextBox。但只是在那个控件都是的那个项目中。

我有BoolToVis转换器。

 <ListView Name="lst1" ItemsSource="{Binding Fields}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox  IsChecked="False" Name="chbVis"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumn.Header>
                        <TextBlock Text="Visibility"></TextBlock>
                    </GridViewColumn.Header>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Name}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumn.Header>
                        <TextBlock Text="Field" Visibility="{Binding Path=IsChecked, ElementName=chbVis, Converter={StaticResource BoolToVis}}"></TextBlock>
                    </GridViewColumn.Header>
                </GridViewColumn>
             </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

1 个答案:

答案 0 :(得分:0)

我通过在Field类中创建一个用于绑定复选框的属性来解决问题。

public bool fieldVis;
    public bool FieldVis
    {
        get { return fieldVis; }
        set
        {
            fieldVis= value;
            NotifyPropertyChanged();
        }
    }

然后xaml看起来像:

 <ListView Name="lst1" ItemsSource="{Binding Fields}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox  IsChecked="{Binding FieldVis}" Name="chbVis"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumn.Header>
                        <TextBlock Text="Visibility"></TextBlock>
                    </GridViewColumn.Header>
                </GridViewColumn>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Name}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                    <GridViewColumn.Header>
                        <TextBlock Text="Field" Visibility="{Binding fieldVis, Converter={StaticResource BoolToVis}}"></TextBlock>
                    </GridViewColumn.Header>
                </GridViewColumn>
             </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

它就像魅力一样。