繁体   English   中英

从ListView中的DataTemplate添加滑块值

[英]Adding Slider values from DataTemplate in ListView

我创建了一个DataTemplate,它在水平堆栈面板中包含:一个TextBlock和一个Slider(x:Name =“ _ score”)。 TextBlock引用新的ObservableCollection,其中Category类包含一个字符串,该字符串是TextBlock文本。 具有7个项目的ListView使用DataTemplate,因此窗口中有7个滑块。

每次更改Slider _score的值时,我都希望使用所有Slider值的总和来更新TextBlock。 不同的滑块将具有用户选择的不同值。 我有一个ValueChanged =“ _ slidScore的事件处理程序。”

我可以将滑条彼此区分开来为每个滑条添加总计吗?

<Page.Resources>
        <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
        <x:String x:Key="AppName">Questionnaire</x:String>
        <DataTemplate x:Key="_itemTemplate">
            <Border BorderBrush="Gainsboro" BorderThickness="4">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="8,0,8,0" FontSize="18" Width="200" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding crit}"/>
                    <StackPanel>
                                        <Slider Margin="10,0,10,0" VerticalAlignment="Center" Value="0" Maximum="5" Minimum="0" TickFrequency="1" TickPlacement="Outside" x:Name="_score" Width="641" ValueChanged="_slidScore"/>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                        <TextBlock VerticalAlignment="Center" Text="Rating: " FontSize="24"/>
                        <TextBlock VerticalAlignment="Center" Margin="10,0,0,0" Text="{Binding Value, ElementName=_score}" FontSize="24"/>
                    </StackPanel>
                </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Page.Resources>


....
in the main grid:

        <Border Grid.Row="1" BorderBrush="Red" BorderThickness="5">
            <TextBlock Text="how often have you..." TextWrapping="Wrap" FontWeight="Bold" FontSize="20" VerticalAlignment="Center"/>
        </Border>
        <ListView x:Name="_listView" Grid.Row="2" ItemTemplate="{StaticResource _itemTemplate}" SelectionMode="None" HorizontalContentAlignment="Stretch"/>
        <TextBlock Grid.Row="2" Grid.Column="1" x:Name="_result" FontSize="70"/>

...

in codebehind file



//list to populate the TextBlock in DataTemplate

_listView.ItemsSource = new ObservableCollection<Category>
            {
                new Category{crit = "1231231231231231231?"},
                new Category{crit = "asdfafhadgfjargfjagj?"},
                new Category{crit = "qerklhjyahkildfjkladnhjkla?"},
                new Category{crit = "13490p76812390-qhjsedhjklg?"},
                new Category{crit = "asdfasdgq3e45uq345u?"},
                new Category{crit = "q3490u8yq38945yasdjiofhj?"},
                new Category{crit = "13406923789045whjioerghjkla?"}
            };



    class Category
    {
        public string crit { get; set; }
    } 

我发现了自己的答案:

    int total = 0;


    private void _slidScore(object sender, RangeBaseValueChangedEventArgs e)
    {
        total -= (int)e.OldValue;
        total += (int)e.NewValue;
        _result.Text = total.ToString();
    }

_slidScore是滑块的事件处理程序。 然后将把int total输出到TextBlock _result。

我将滑块值存储在Category类中:

private int _value;
public int value
{
    get { return _value; }
    set
    {
        if (value == _value) return;
        _value = value;
        OnPropertyChanged();
    }
}

我将计算逻辑放在ViewModel类中:

private int _totalValue;
public int TotalValue
{
    get { return _totalValue; }
    private set
    {
        if (value == _totalValue) return;
        _totalValue = value;
        OnPropertyChanged();
    }
}

private ObservableCollection<Category> _categories;
public ObservableCollection<Category> Categories
{
    get { return _categories; }
    set
    {
        if (Equals(value, _categories)) return;
        DetachHandlers(_categories); // not necessary if collection won't change after constructing ViewModel
        _categories = value;
        AttachHandlers(_categories); // could be only in constructor if collection won't cahnge later
        OnPropertyChanged();
    }
}

private void DetachHandlers(ObservableCollection<Category> categories)
{
    // releases existing handlers
    if (categories == null)
    {
        return;
    }

    foreach (var category in categories)
    {
        category.PropertyChanged -= CategoryOnPropertyChanged;
    }

    categories.CollectionChanged -= CategoriesOnCollectionChanged;
}

private void AttachHandlers(ObservableCollection<Category> categories)
{
    if (categories == null)
    {
        return;
    }

    foreach (var category in categories)
    {
        // crucial: triggers when slider values change
        category.PropertyChanged += CategoryOnPropertyChanged;
    }

    // necessary only if categories in the collection change later
    categories.CollectionChanged += CategoriesOnCollectionChanged;
}

private void CategoriesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
    // detach handlers from removed categories
    foreach (Category category in notifyCollectionChangedEventArgs.OldItems)
    {
        category.PropertyChanged -= CategoryOnPropertyChanged;
    }
    // attach handlers to added categories
    foreach (Category category in notifyCollectionChangedEventArgs.NewItems)
    {
        category.PropertyChanged += CategoryOnPropertyChanged;
    }
}

private void CategoryOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
    if (propertyChangedEventArgs.PropertyName == "value")
    {
        // slider value changed: recalculate
        TotalValue = Categories.Sum(c => c.value);
    }
}

基本上,以上代码附加了一个事件处理程序,该事件处理程序在每次类别值更改时触发并重新计算总数。

您需要向现有XAML添加一些绑定:

<Slider x:Name="_score" Value="{Binding value, Mode=TwoWay}" />

<ListView x:Name="_listView" ItemsSource="{Binding Categories}" />

<TextBlock x:Name="_result" Text="{Binding TotalValue}" />

当然在后面的代码中将ViewModel设置为DataContext

DataContext = new ViewModel
    {
        Categories = new ObservableCollection<Category>
            {
                new Category {crit = "1231231231231231231?"},
                new Category {crit = "asdfafhadgfjargfjagj?"},
                new Category {crit = "qerklhjyahkildfjkladnhjkla?"},
                new Category {crit = "13490p76812390-qhjsedhjklg?"},
                new Category {crit = "asdfasdgq3e45uq345u?"},
                new Category {crit = "q3490u8yq38945yasdjiofhj?"},
                new Category {crit = "13406923789045whjioerghjkla?"}
            }
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM