English 中文(简体)
如何在WPF中从ListView获取绑定的ListboxItems
原标题:How to get binded ListboxItems from ListView in WPF

我对WPF还比较新,对于任何愚蠢的问题请原谅我...

我有一个带有三列的ListView,其绑定到以下XmlDataProvider源:

        <XmlDataProvider x:Key="Properties" XPath="/Info">
        <x:XData>
            <Info xmlns="">
                <Property Name="Text" Value=""/>                <!--0-->
                <Property Name="Tooltip" Value=""/>             <!--1-->
                <Property Name="Enable" Value=""/>              <!--2-->
                <Property Name="Visible" Value=""/>             <!--3-->
                <Property Name="Focus" Value=""/>               <!--4-->
                <Property Name="Selected" Value=""/>            <!--5-->
                <Property Name="Count" Value=""/>               <!--6-->
                <Property Name="Item" Value=""/>                <!--7-->
                <Property Name="SelectedText" Value=""/>        <!--8-->
                <Property Name="SelectedIndex" Value=""/>       <!--9-->
                <Property Name="Complete" Value=""/>            <!--10-->
            </Info>
        </x:XData>
    </XmlDataProvider>

ListView的定义如下:

        <ListView Name="lstProperties"  Margin="55 0 0 0" Style="{DynamicResource TsListView}"
        Grid.Row="2" Grid.RowSpan="7" Grid.ColumnSpan="4"
        ItemsSource="{Binding Source={StaticResource Properties}, XPath=Property}" 
        ItemContainerStyle="{DynamicResource TsListViewItem}" 
        ScrollViewer.HorizontalScrollBarVisibility="Hidden"
        SelectionMode="Single" IsEnabled="False"
        SelectionChanged="propertySelected" 
        >

        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn CellTemplate="{StaticResource FirstCell}" Width="25" />
                <GridViewColumn Header="Property" Width="80">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Label Style="{DynamicResource TsLabel}" Height="25" Width="115" Content="{Binding XPath=@Name}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                <GridViewColumn Header="Value" Width="130">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Style="{DynamicResource TsHelperTextBox}"
                                     Height="20" Width="115" Text="{Binding XPath=@Value}" 
                                     IsEnabled="{Binding ElementName=rbTypeAssert, Path=IsChecked}" GotFocus="gridTextBox_GotFocus" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

What I want to do now is fairly simple: I just want to enable/disable some of the ListViewItems. The only way I found to get ListViewItems is through the following method:

lstProperties.ItemContainerGenerator.ContainerFromIndex(index)

This makes me a little uncomfortable. I should be getting the Items through the name property of Property. Is there anyway to do this? I m also having problems when I try to do this right after the window is initialized. I get a NullReferenceException when trying to disable one of these ListViewItems. It seems that right after the window is rendered the binding is not done yet.

最佳回答

你的反馈与其他收到的反馈结合起来,让我解决了这个问题。非常感谢!

我所做的是,在XmlDataProvider的不动产上添加一个新特性,名称是可实现的,然后,我将清单中可变财产归为这一新特性。 因此,我没有像你所建议的那样拥有一个全球财产来界定所有清单记录的状况,而是拥有“个人约束”。

改变方案一级使用以下方法的数据:

provider.Document.SelectSingleNode("//Property[Name="Text"]/IsEnable").InnerText = false.ToString();

这种方法需要略微不同的数据格式。因此,XmlDataProvider 现在如下:

        <XmlDataProvider x:Key="Properties" XPath="/Info" IsAsynchronous="False" IsInitialLoadEnabled="True">
        <x:XData>
            <Info xmlns="">
                <Property>
                    <Name>Text</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Tooltip</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Enable</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Visible</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Focus</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Selected</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Count</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Item</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>SelectedText</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>SelectedIndex</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
                <Property>
                    <Name>Complete</Name>
                    <Value></Value>
                    <IsEnable>true</IsEnable>
                </Property>
            </Info>
        </x:XData>
    </XmlDataProvider>

Thanks again for your contribution! José Tavares

问题回答

肯特的答案是正确的,但我会使用转换器,而不是扩展您的模型:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsEnabled" Value="{Binding Converter={StaticResource IsEnabledConverter}}"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

转换器:

public class IsEnabledConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Implement your logic here, and return true/false accordingly
        return true;
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }

    #endregion
}

这样做的最容易和最佳方式是暴露确定<代码>是否为之的财产。 清单: 应当做到:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsEnabled" Value="{Binding YourProperty}"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签