Wpf ListView,文本框用作过滤器

时间:2016-06-03 08:45:44

标签: c# wpf

我有listview,每个列都有一个文本框。如何将文本框宽度附加到适当的listview列宽度? 示例代码:

<ListView x:Name="listView" Margin="10,41,10,10">
        <ListView.View >
            <GridView>
                <GridViewColumn Header="ASD">
                </GridViewColumn>
                <GridViewColumn Header="ASD2">
                </GridViewColumn>
                <GridViewColumn Header="ASD3">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="10,14,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="97"/>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="107,14,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="93"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="200,14,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

2 个答案:

答案 0 :(得分:1)

如果要绑定到该值,可以使用:

<ListView>
    <ListView.View >
        <GridView>
            <GridViewColumn x:Name="gvc1" Header="ASD" Width="{Binding ElementName=textBox, Path=Text}">
            </GridViewColumn>
                <GridViewColumn x:Name="gvc2" Header="ASD2">
            </GridViewColumn>
                <GridViewColumn x:Name="gvc3" Header="ASD3">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

否则:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ASD" Width="{Binding ElementName=textBox, Path=Width}">
            </GridViewColumn>
            <GridViewColumn Header="ASD2" Width="{Binding ElementName=textBox1, Path=Width}">
            </GridViewColumn>
            <GridViewColumn Header="ASD3" Width="{Binding ElementName=textBox2, Path=Width}">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

相反的方式

<ListView>
    <ListView.View >
        <GridView>
            <GridViewColumn x:Name="gvc1" Header="ASD">
            </GridViewColumn>
                <GridViewColumn x:Name="gvc2" Header="ASD2">
            </GridViewColumn>
                <GridViewColumn x:Name="gvc3" Header="ASD3">
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
<StackPanel Grid.Row="1">
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="{Binding ElementName=gvc1, Path=Width}"/>
    <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="{Binding ElementName=gvc2, Path=Width}"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="{Binding ElementName=gvc3, Path=Width}"/>
</StackPanel>

答案 1 :(得分:0)

您可以在页面中创建一个资源,该资源将定义宽度并在文本框和列宽中使用此资源。 以下是可以帮助您的内容:Specify width/height as resource in WPF