如何在ControlTemplate中绑定?

时间:2015-08-24 12:12:48

标签: wpf xaml listview binding uwp

我有ListView,内容为List<MyListItem>,我需要使用ControlTemplate才能在选择项目时更改效果。现在我遇到{Binding MyProperty}ControlTemplate内部无效的问题。如何在模板中访问MyListItem的属性?

我的XAML看起来像这样(简化):

<ListView
        Name="ListView"
        Grid.Column="1"
        IsSwipeEnabled="False">

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualState x:Name="Unselected">
                                            <Storyboard>
                                                <ColorAnimation Duration="0" Storyboard.TargetName="myColoredText" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Orange"/>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="SelectedUnfocused">
                                            <Storyboard>
                                                <ColorAnimation Duration="0" Storyboard.TargetName="myColoredText" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>

                                <!-- Here I have my custom layout, removed for readability -->
                                <TextBlock
                                    Name="myColoredText"
                                    Foreground="Green"
                                    Text="{Binding MyProperty}"/><!-- This does not work -->

                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

以下是此XAML背后的课程:

public sealed partial class MyPage : Page
{
    public MyPage()
    {
        InitializeComponent();
        ListView.ItemsSource = new List<MyListItem>()
        {
            new MyListItem("Title1", "SubTitle1"),
            new MyListItem("Title2", "SubTitle2")
        }
    }
}

和MyListItem类:

class MyListItem
{
    public string MyProperty { get; set; }
    public string MyOtherProperty { get; set; }

    public MyListItem(string myProperty, string myOtherProperty)
    {
        MyProperty = myProperty;
        MyOtherProperty = myOtherProperty;
    }
}

问题最小的项目:

Project

4 个答案:

答案 0 :(得分:9)

TextBox中,使用:

Text="{Binding Content.MyProperty, 
               RelativeSource={RelativeSource TemplatedParent}}"

答案 1 :(得分:1)

Wpf仅支持绑定属性,而不支持绑定到字段。所以,MyItemClass应该是:

public class MyListItem { 
    public string MyProperty { get; set; } 
    public string MyOtherProperty { get; set; } 

    public MyListItem(string myProperty, string myOtherProperty) {         
        MyProperty = myProperty; 
        MyOtherProperty = myOtherProperty;  
    }
}

希望,这有帮助

答案 2 :(得分:0)

在处理自定义{TemplateBinding}时,您需要使用ControlTemplate表达式。这会将属性值绑定到与控件实例关联的任何相应属性。

<TextBlock Name="myColoredText"
           Foreground="Green"
           Text="{TemplateBinding MyProperty}" />

这将直接绑定到控件中的指定实例属性。由于WPF / WinRT控件为DependencyObjects,因此常用DependencyProperties

public static readonly DependencyProperty MyPropertyProperty = 
    DependencyProperty.Register(
        "MyProperty",
        typeof(Boolean),
        typeof(ListViewItem),
        null
    );

public bool MyProperty
{
    get { return (bool)GetValue(MyPropertyProperty); }
    set { SetValue(MyPropertyProperty, value); }
}

然后在声明一个新实例时,会在XAML中标准填充此属性:

<someNamespace:ListViewItem MyProperty={Binding ViewModelProperty} />

您可以在MSDN上阅读有关TemplateBinding标记表达式的更多信息。

答案 3 :(得分:0)

ControlTemplate通常用于定义控件的外观。因此,将XAML放入DataTemplate并将其分配给ListView.ItemTemplate属性要容易得多。您可以从DataTemplate

访问所有自定义属性
<ListView.ItemTemplate>
    <DataTemplate>
        <TextBlock Name="myColoredText" Foreground="Green"
            Text="{Binding MyProperty}" /><!-- This will now work -->
    </DataTemplate>
</ListView.ItemTemplate>

更新&gt;&gt;&gt;

您还可以使用DataTemplate属性中的ItemContainerStyle,如下所示:

<Style x:Key="SomeStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate DataType="{x:Type Your:DataType}">
                <TextBlock Name="myColoredText" Foreground="Green"
                    Text="{Binding MyProperty}" /><!-- This will now work -->
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>