2010-02-22 54 views
1

绑定ListboxItems我很新的WPF所以原谅我的任何愚蠢的问题...如何从ListView控件在WPF

我有被绑定到一个XmlDataProvider源,如以下三列一个ListView :

 <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 [email protected]}" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       <GridViewColumn Header="Value" Width="130"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox Style="{DynamicResource TsHelperTextBox}" 
            Height="20" Width="115" Text="{Binding [email protected]}" 
            IsEnabled="{Binding ElementName=rbTypeAssert, Path=IsChecked}" GotFocus="gridTextBox_GotFocus" /> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
      </GridView> 
     </ListView.View> 
    </ListView> 

我想现在做的是相当简单:我只是想启用/禁用某些ListViewItems的。 我找到致富ListViewItems的唯一方法是通过下面的方法:

lstProperties.ItemContainerGenerator.ContainerFromIndex(index) 

这让我有点不舒服。我应该通过Property的名称属性获取项目。无论如何要做到这一点? 当我尝试在窗口初始化后立即执行此操作时,我也遇到问题。当试图禁用这些ListViewItems中的一个时,我得到一个NullReferenceException。看起来在窗口呈现之后,绑定还没有完成。

回答

0

好您的意见,与其他反馈合成接收,让我解决了这个问题。非常感谢!

我所做的是在XmlDataProvider的属性中添加一个名为IsEnable的新属性,然后将ListViewItem的IsEnabled属性绑定到这个新属性。因此,如同你所建议的那样,而不是让一个全局属性来定义所有ListViewItems的状态,我会有“个人绑定”。

以编程方式更改数据I使用以下方法:

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> 

再次感谢您的贡献! 何塞·塔瓦雷斯

1

做到这一点的最简单有效的方法是揭露确定的ListViewItem是否应该启用与否的属性,然后绑定到它:

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

谢谢,我已经想到了什么像这样,但我用这种方法有三个问题 。 1.如何定义一个自定义属性(OK这真的是一个新手问题) 2.我已经有一个为我的应用程序中的所有ListViewItems定义的样式。你的解决方案是否意味着我必须为这些ListViewItems有特定的风格?最重要的是,3.在这种特殊情况下,我需要每个ListViewItem一个属性,而不是一个普通属性。我可以从XmlDataSource绑定这个吗? 谢谢 – jpsstavares 2010-02-22 20:49:23

1

肯特的答案是正确的,但我会用一个转换器,而不是扩大你的模型:

<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 
} 
+0

嗯,我看到这个工作,虽然我认为我更喜欢我得到的解决方案,因为绑定更清晰,并且不涉及其他代码。但是,谢谢你的提示,它可能会在其他情况下对我有用。 – jpsstavares 2010-02-24 15:07:15