GridView:NumericUpDown 列更新第二列

发布于 2024-09-16 19:19:47 字数 4103 浏览 12 评论 0原文

使用 gridview,我有一列带有绑定到底层对象列表的 NumericUpDown 控件。问题是我可以绑定到 UpDown 控件上的 valuechanged 事件,但是当我更新百分比时,它不会在屏幕(GridView)上更新。如果在member.refreshCandidatesGrid中我调用grid.Rebind(),它会导致混乱(屏幕锁定)。如果我在侧面添加一个按钮,并在设置所有值后单击该按钮,并让该按钮调用 member.refreshCandidatesGrid 并重新绑定,everythgin 工作正常。

我希望有两种方法中的一种起作用:

  1. 推动微调器(数字向上向下),并触发一个事件(微调器中的每个刻度),我可以用它来更新百分比

  2. 当我离开该微调器字段时会触发事件,不幸的是,LostFocus 看起来是唯一的事件可能有意义,并且似乎在旋转器的起诉期间触发...

对方法、代码结构(即使不相关)的想法都表示赞赏 - 提前谢谢您。

XAML 看起来像这样:

<telerik:RadGridView x:Name="candidateGrid" DataContext="{Binding}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Score" IsFilterable="False"
                        DataMemberBinding="{Binding score, Mode=TwoWay}" Width="80" >
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding score}" HorizontalAlignment="Right"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <telerik:RadNumericUpDown Loaded="Editor_Loaded" Maximum="10000" UpdateValueEvent="PropertyChanged"  IsInteger="True"  Minimum="0" ValueChanged="score_ValueChanged"
                                    Value="{Binding score, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Width="60" O />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Percent" IsReadOnly="True" IsFilterable="False" Width="70">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding percent}" HorizontalAlignment="Right"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>

并且它绑定到一个 f# 类,如下所示:

namespace BV

open System
open System.Text.RegularExpressions
open System.Windows ;
open System.Windows.Controls;
open System.Windows.Controls.Primitives ;
open System.Windows.Media ;
open Telerik.Windows.Controls;

open Globals
open RpcBV

type Page() as this =    
    inherit UriUserControl("/BV;component/page.xaml", "page")

    // instance data
    let mutable brokers = RpcBV.getCandidates()

    // bound controls for add candidate
    let FE : FrameworkElement = (this.Content :?> FrameworkElement)
    let candidateGrid : RadGridView = FE ? candidateGrid

    do
        firm.ItemsSource <- RpcBV.getFirms()
        email.Background <- SolidColorBrush(Colors.Red)
        addBtn.IsEnabled <- false
        this.refreshCandidatesGrid() ;

    member this.refreshCandidatesGrid () =
        candidateGrid.ItemsSource <- brokers ;
        ()



    member this.calculatePercentages() =
        let totalScore = List.fold (fun acc (candidate: ScorableCandidate) -> acc + candidate.score) 0 brokers

        let scoreEm (candidate: ScorableCandidate) =
             candidate.percent <- (float candidate.score) / float totalScore * 100.

        List.iter scoreEm <| brokers

    member this.addCadidate_Click (sender : obj) (args : RoutedEventArgs)  = 
        this.addCandidateFromForm()
        this.resetAddCandidateForm()
        this.refreshCandidatesGrid()

    member this.score_LostFocus (sender : obj) (args : RoutedEventArgs)  = 
        ()

    member this.score_ValueChanged (o : obj) (arg : RadRangeBaseValueChangedEventArgs) = 
        this.calculatePercentages()

    member this.Editor_Loaded (sender:obj) (args: RoutedEventArgs) =
        let ctrl = sender :?> Control;
        ctrl.Focus() |> ignore ;
        ()

Using a gridview I a have one column with a NumericUpDown control bound to an underlying list of objects. The problem is I can bind to the valuechanged eventon the UpDown control, however when I update the percentages, it doesn't update on the screen (GridView). If in member.refreshCandidatesGrid i call grid.Rebind() it causes a mess (screen locks up). If I add a button off to the side and click that after I set all the values and have that call member.refreshCandidatesGrid with the rebind everythgin works fine.

I would love to have 1 of 2 approaches work:

  1. push the spinner (numeric updown), and have that fire an event (per tick in the spinner) that I can use to update the percentages

  2. have an event fire when i leave that spinner field, unfortunately the LostFocus looks to be the only event that may make sense and seems to fire during the sue of the spinner...

Thoughts on approach, code structure (even if unrelated) are all appreciated - thank you in advance.

The XAML looks somesthing like this:

<telerik:RadGridView x:Name="candidateGrid" DataContext="{Binding}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Score" IsFilterable="False"
                        DataMemberBinding="{Binding score, Mode=TwoWay}" Width="80" >
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding score}" HorizontalAlignment="Right"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                    <telerik:GridViewDataColumn.CellEditTemplate>
                        <DataTemplate>
                            <telerik:RadNumericUpDown Loaded="Editor_Loaded" Maximum="10000" UpdateValueEvent="PropertyChanged"  IsInteger="True"  Minimum="0" ValueChanged="score_ValueChanged"
                                    Value="{Binding score, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Width="60" O />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellEditTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Percent" IsReadOnly="True" IsFilterable="False" Width="70">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding percent}" HorizontalAlignment="Right"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>

and it is bound to an f# class like the following:

namespace BV

open System
open System.Text.RegularExpressions
open System.Windows ;
open System.Windows.Controls;
open System.Windows.Controls.Primitives ;
open System.Windows.Media ;
open Telerik.Windows.Controls;

open Globals
open RpcBV

type Page() as this =    
    inherit UriUserControl("/BV;component/page.xaml", "page")

    // instance data
    let mutable brokers = RpcBV.getCandidates()

    // bound controls for add candidate
    let FE : FrameworkElement = (this.Content :?> FrameworkElement)
    let candidateGrid : RadGridView = FE ? candidateGrid

    do
        firm.ItemsSource <- RpcBV.getFirms()
        email.Background <- SolidColorBrush(Colors.Red)
        addBtn.IsEnabled <- false
        this.refreshCandidatesGrid() ;

    member this.refreshCandidatesGrid () =
        candidateGrid.ItemsSource <- brokers ;
        ()



    member this.calculatePercentages() =
        let totalScore = List.fold (fun acc (candidate: ScorableCandidate) -> acc + candidate.score) 0 brokers

        let scoreEm (candidate: ScorableCandidate) =
             candidate.percent <- (float candidate.score) / float totalScore * 100.

        List.iter scoreEm <| brokers

    member this.addCadidate_Click (sender : obj) (args : RoutedEventArgs)  = 
        this.addCandidateFromForm()
        this.resetAddCandidateForm()
        this.refreshCandidatesGrid()

    member this.score_LostFocus (sender : obj) (args : RoutedEventArgs)  = 
        ()

    member this.score_ValueChanged (o : obj) (arg : RadRangeBaseValueChangedEventArgs) = 
        this.calculatePercentages()

    member this.Editor_Loaded (sender:obj) (args: RoutedEventArgs) =
        let ctrl = sender :?> Control;
        ctrl.Focus() |> ignore ;
        ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-09-23 19:19:48

如果您希望属性更改立即反映在 UI 中,那么控件绑定的类型可能应该实现 INotifyPropertyChanged 接口(或使用其他机制,例如公开 DependencyProperties ) )。您对 ScorableCandidate 的定义是什么样的?

If you want property changes to be reflected immediately in the UI then the type that your controls are bound to should probably implement the INotifyPropertyChanged interface (or use another mechanism such as exposing DependencyProperties). What does your definition of ScorableCandidate look like?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文