Silverlight更新/触发在DataGrid的Listbox DataTemplate中的IValueConverter [英] Silverlight Update/Trigger IValueConverter in Listbox DataTemplate in a DataGrid

查看:226
本文介绍了Silverlight更新/触发在DataGrid的Listbox DataTemplate中的IValueConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序来显示绑定到一个ObservableCollection记录的datagrid,其中每个记录都有一个课程对象和一个ObservableCollection的结果对象。



课程使用自动填充框更改。
结果集合显示在具有IValueConverter实现的列表框中,以根据当前选定的课程的条件更改椭圆模板的颜色。



它的工作原理很好的加载,但通过自动完成对课程选择的后续更新不会触发重新计算/刷新值转换器。



有没有办法触发刷新XAML我添加UpdateSource =属性更改为列表框的绑定 - 但这会导致堆栈溢出(haha)。



这是代码:

 < data:DataGrid x:Name =MyDatGrid> 
< data:DataGrid.Columns>
< data:DataGridTemplateColumn Header =Results>
< data:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< ListBox ItemsSource ={Binding ListOfResults}>
< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel Orientation =Horizo​​ntal/>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>
< ListBox.ItemTemplate>
< DataTemplate>
< Ellipse Width =20Height =20Fill ={Binding Converter = {StaticResource resultToBrushConverter}}Stroke =BlackStrokeThickness =1/>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
< / DataTemplate>
< / data:DataGridTemplateColumn.CellTemplate>
< / data:DataGridTemplateColumn>
< data:DataGridTemplateColumn Header =Course>
< data:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Border>
< input:AutoCompleteBox ItemsSource ={Binding Courses,Source = {StaticResource coursesSource}}/>
< / Border>
< / DataTemplate>
< / data:DataGridTemplateColumn.CellTemplate>

我设法在自动填充框中订阅了LostFocus事件,并重置了我已经拥有的过滤器数据网格但是,这是非常低效的?
刷新datagrid上的视图对该方法没有任何影响。



正确方向的任何步骤都将非常感谢。试图阻止自己再变灰:)



有没有想法在网格中获取列表的绑定表达式并更新它,但不知道?



感谢你们

解决方案

首先确保Record对象实现INotifyPropertyChanged,Course属性调用



resultToBrushConverter中的转换器是否需要访问绑定对象的多个属性?如果它仅仅依赖于课程,而不是使用课程作为其路径。



修改:假设你确实有这些东西,你对Fill的约束应该如下所示: -

 < Ellipse Width =20Height =20Fill ={Binding Coarse,Converter = {StaticResource resultToBrushConverter}}Stroke =BlackStrokeThickness =1/> 

现在,绑定知道监控Coarse属性,当PropertyChanged事件触发时,它应该更新Fill粗体财产。


I am building an application to display a datagrid bound to an ObservableCollection of Records, where each record has a Course Object and an ObservableCollection of Results Objects.

The course is changed using an autocomplete box. The results collection is displayed in a Listbox with an IValueConverter implementation to change the colour of the ellipse template based on criteria of the course currently selected.

It works great on loading, but subsequent updates to the course selection via the autocomplete does not trigger a recalculation/refresh of the value converter.

Is there a way to trigger the refresh in XAML. I added UpdateSource=Property changed to the binding of the list box - but this caused a stack overflow (haha).

Here is the code:

<data:DataGrid x:Name="MyDatGrid">
<data:DataGrid.Columns>
    <data:DataGridTemplateColumn Header="Results">
        <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           <ListBox ItemsSource="{Binding ListOfResults}">
               <ListBox.ItemsPanel>
                   <ItemsPanelTemplate>
                       <StackPanel Orientation="Horizontal"/>
                   </ItemsPanelTemplate>
               </ListBox.ItemsPanel>
               <ListBox.ItemTemplate>
                   <DataTemplate>
                       <Ellipse Width="20" Height="20" Fill="{Binding Converter={StaticResource resultToBrushConverter} }" Stroke="Black" StrokeThickness="1" />
                   </DataTemplate>
               </ListBox.ItemTemplate>
          </ListBox>
        </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate>
     </data:DataGridTemplateColumn>
     <data:DataGridTemplateColumn Header="Course" >
        <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
               <Border>
                   <input:AutoCompleteBox ItemsSource="{Binding Courses, Source={StaticResource coursesSource}}"/>
               </Border>
             </DataTemplate>
     </data:DataGridTemplateColumn.CellTemplate>

I managed to subscribe to the LostFocus Event on the autocomplete box and reset a filter that I already have on the datagrid. But isn;t this very inefficient ? Refreshing the view on the datagrid does not have any effect in that method.

Any steps in the right direction are greatly appreciated. Trying to prevent myself going anymore grey :)

Had thoughts of getting the binding expression of the list in the grid and updating it, but no clue ?

Thanks guys

解决方案

First ensure that the Record object implements INotifyPropertyChanged and the Course property invokes it.

Does the converter in resultToBrushConverter require access to more than one property of the bound object? If it depends only on Course than use Course as its path.

Edit:

Assuming you do have these things in place your binding on Fill should look like:-

<Ellipse Width="20" Height="20" Fill="{Binding Coarse, Converter={StaticResource resultToBrushConverter} }" Stroke="Black" StrokeThickness="1" />

Now that the binding knows its monitoring the Coarse property it should update the Fill when the PropertyChanged event fires for the Coarse property.

这篇关于Silverlight更新/触发在DataGrid的Listbox DataTemplate中的IValueConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆