WPF UserControl公开内部列表项 [英] WPF UserControl exposing inner listitems

查看:54
本文介绍了WPF UserControl公开内部列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图编写一个UserControl来显示项目列表,其中每个项目都是标题和一组复选框。整体将代表一种数据形式,其中填写数据的人将使用1到4的值回答一系列问题。

I'm trying to write a UserControl to display a list of items where each of these items is a title and a group of checkboxes. This whole will represent a form of data where the person filling it in is answering a list of questions with a 1 to 4 value. This all works and binds nicely to the window's ViewModel.

但是我目前在UserControl中得到的答案如下:

But I've currently got the answers hardcoded in the UserControl as follows:

<ListBox
  ItemsPanel="{StaticResource HorizontalScores}"
  Style="{StaticResource styleOuterListBox}"
  ItemContainerStyle="{StaticResource styleOuterListBoxItem}">
  <ListBoxItem>Never</ListBoxItem>
  <ListBoxItem>Sometimes</ListBoxItem>
  <ListBoxItem>Often</ListBoxItem>
  <ListBoxItem>Always</ListBoxItem>
</ListBox>

我想从窗口的XAML或ViewModel中进行设置,因为其他设置会有所不同形式,但看不到正确的咒语。如何从UserControl中删除ListBoxItems并改用数据绑定?

I would like to set these from the window's XAML or from the ViewModel as they will be different for other forms but can't see the correct incantation. How do I remove the ListBoxItems from the UserControl and use databinding instead?

BigEdit ...

BigEdit ...

好吧,这是实际的用户控件(看起来很丑,但这不是点):

Ok, this is the actual user control (it looks hideous but that's not the point):

<UserControl x:Class="BussPerry.Scorer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:BussPerry.ViewModel" xmlns:local="clr-namespace:BussPerry">

  <UserControl.Resources>

    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="Gray" />
    <SolidColorBrush x:Key="SelectedForegroundBrush" Color="Red" />

    <ItemsPanelTemplate x:Key="HorizontalScores">
      <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>

    <Style x:Key="styleListBox" TargetType="{x:Type ListBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBox}">
            <ItemsPresenter Margin="2" />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="styleListBoxItem" TargetType="{x:Type ListBoxItem}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <CheckBox Name="CheckBox" Padding="1" Width="60" 
                          IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">
              <ContentPresenter HorizontalAlignment="Center"/>
            </CheckBox>
            <ControlTemplate.Triggers>
              <Trigger Property="IsSelected" Value="True">
                <Setter TargetName="CheckBox" Property="Background" Value="{StaticResource SelectedBackgroundBrush}" />
                <Setter TargetName="CheckBox" Property="Foreground" Value="{StaticResource SelectedForegroundBrush}" />
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

  </UserControl.Resources>

  <ListBox ItemsPanel="{StaticResource HorizontalScores}" Style="{StaticResource styleListBox}" 
      ItemContainerStyle="{StaticResource styleListBoxItem}" SelectedIndex="{Binding Path=Score}">
    <ListBoxItem>Never</ListBoxItem>
    <ListBoxItem>Sometimes</ListBoxItem>
    <ListBoxItem>Often</ListBoxItem>
    <ListBoxItem>Always</ListBoxItem>
  </ListBox>

</UserControl>

其名称如下:

<ListView
  Name="listviewScores"
  ItemsSource="{Binding Path=Scores}"

  Margin="5"
  BorderThickness="0"
  Background="Transparent"
  Focusable="False"
  Grid.Row="3">
  <ListView.View>
    <GridView
      ColumnHeaderContainerStyle="{StaticResource styleHiddenHeader}">
      <GridView.Columns>

        <GridViewColumn>
          <GridViewColumn.CellTemplate>
            <DataTemplate>
              <TextBlock
                Text="{Binding Path=Index}"
                HorizontalAlignment="Right" />
              </DataTemplate>
          </GridViewColumn.CellTemplate>
        </GridViewColumn>

        <GridViewColumn
          DisplayMemberBinding="{Binding Path=Title}" />

        <GridViewColumn >
          <GridViewColumn.CellTemplate>
            <DataTemplate>
              <local:Scorer >
              </local:Scorer>
            </DataTemplate>
          </GridViewColumn.CellTemplate>
        </GridViewColumn>
      </GridView.Columns>
    </GridView>
  </ListView.View>

</ListView>

我要做的是将Never / Sometimes /经常/ Always列表框项目从硬编码中移出

What I want to do is to move the Never/Sometimes/Often/Always listboxitems from being hard coded in the user control to be databound.

(也建议您不想那样做!)

(Suggestions of "you don't want to do it like that" are also welcome!)

推荐答案

(一年后...)

我认为您的问题与我的相似。我想出了一种在UserControl上公开内部控件的ItemsSource的技术。我的问题的链接在这里:

I think your question is similar to mine. I have come up with a technique to expose the ItemsSource of an inner control on a UserControl. The link to my question is here:

Exposing inner Control properties for binding in WPF

我知道我的解决方案有效。我不知道这是否违反了WPF中某些神圣的最佳做法。

I know that my solution works. What I don't know is if it violates some sacred 'best practices' out there in WPF. It 'feels' right though.

这篇关于WPF UserControl公开内部列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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