从自定义属性绑定

时间:2008-12-09 19:03:39

标签: silverlight

我是论坛的新手,也是银光的新手。我和我的同事正在开发一个自定义应用程序。我们正在构建一个菜单系统,只有在指定角色中使用该按钮时才会显示按钮。创建了一个新属性以允许定义角色,出于测试目的,我们只是尝试将该值(字符串)分配给textblock的text属性。附上了一些代码。

这是要添加到集合中的项目之一。 allowedroles属性正在传递字符串,这可以通过调试器看到。

<MenuButton:VerticalButtonCollection x:Key="VerticalButtonsDS" d:IsDataSource="True">

<MenuButton:VerticalButton AllowedRoles="test, test2"> 
<TextBlock Text="{Binding AllowedRoles}"></TextBlock>

</MenuButton:VerticalButton>

</MenuButton:VerticalButtonCollection>

Code for the allowed roles property  
Public Shared ReadOnly AllowedRolesProperty As DependencyProperty = DependencyProperty.Register("AllowedRoles", GetType(String), GetType(mButton), New PropertyMetadata(New PropertyChangedCallback(AddressOf onAllowedRolesChanged)))


Public Shared Sub onAllowedRolesChanged(ByVal d As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs) 
     Dim sender As mButton = CType(d, mButton)
     sender.AllowedRoles = CStr(args.NewValue)
End Sub

项目显示在列表框中,没有错误,但绑定不起作用。我甚至试图在列表框的数据模板中进行绑定。我道歉,如果这令人困惑,我不知道如何以易于理解的方式发布这样的东西。

由于

4 个答案:

答案 0 :(得分:0)

使用Binding语法时,您将绑定到对象的DataContext。所以你的约束力:

<MenuButton:VerticalButton AllowedRoles="test, test2"> 
  <TextBlock Text="{Binding AllowedRoles}"></TextBlock>
</MenuButton:VerticalButton>

基本上是说我的DataContext上的属性“AllowedRoles”。由于数据上下文可能为null(没有看到它被设置),因此您的文本块中没有任何值。

你可能想要做的是为MenuButton定义一个模板,然后在模板中你可以说:

<TextBlock Text="{TemplateBinding AllowedRoles}"></TextBlock>

将模板应用于项目时将设置哪个。

以下是我在理解模板和generic.xaml的另一个帖子中发布的一些链接:

  • 控制定制
  • Skinnable自定义控件
  • 调整默认样式
  • generic.xaml上有3个很好的链接

祝你好运!

答案 1 :(得分:0)

<MenuButton:VerticalButton AllowedRoles="test, test2"> 

以上行是指定了allowedroles值的位置。我正在创建一个新的按钮实例。

我试图将允许的角色绑定到文本块可能完全错误,但即使它在列表框的datatemplate中完成也无法正常工作。

<DataTemplate x:Key="VerticalMenuItemTemplate">
            <Canvas>
            <TextBlock Text="{Binding AllowedRoles}">
            </TextBlock>
            <Rectangle Height="500" Width="500" Fill="Blue"></Rectangle></Canvas>
        </DataTemplate>

事实上,正如你从这个小片段中看到的那样,我有一个矩形设置和画布。我没有看到rect,它仅用于测试目的,所以它几乎就像我的模板被忽略了。我将列表框设置为folows

<ListBox x:Name="VerticalContainer" Width="81" Height="Auto" HorizontalAlignment="Center" DataContext="{Binding Mode=OneWay, Source={StaticResource VerticalButtonCollectionDS}}" Padding="0,0,0,0" Canvas.Top="-14" ItemTemplate="{StaticResource VerticalMenuItemTemplate}">

答案 2 :(得分:0)

这是我们尝试做的一个小例子。我可以获得“VerticalButton”的内容并显示它很好,但是我们需要能够获得模板中的“AllowedRoles”依赖项属性,因为最终目标是将项目的可见性绑定到此属性(之后)创建一个IValueConverter)。我们似乎无法围绕如何从列表框的ItemTemplate中获取AllowedRoles模板。

这是Page.xaml:

<UserControl x:Class="AnnexAMapTool.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mt="clr-namespace:AnnexAMapTool;assembly=AnnexAMapTool"
    Width="400" Height="300">
    <UserControl.Resources>

        <mt:VerticalButtonCollection x:Key="TestCollection">
            <mt:VerticalButton AllowedRoles="Test1, Test2" Content="VBContent"></mt:VerticalButton>
        </mt:VerticalButtonCollection>

        <Style x:Key="ListItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel x:Name="Root" Orientation="Horizontal">
                            <TextBlock Foreground="Red" Text="{Binding AllowedRoles}"></TextBlock>
                            <Ellipse Width="20" Height="20" Fill="Blue"></Ellipse>
                            <ContentPresenter Content="{TemplateBinding Content}" Margin="5,5,5,5"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ListStyle" TargetType="ListBox">
            <Setter Property="ItemContainerStyle" Value="{StaticResource ListItemStyle}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <Grid x:Name="Root">
                            <ItemsPresenter></ItemsPresenter>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <ListBox Style="{StaticResource ListStyle}" ItemsSource="{StaticResource TestCollection}" />
    </Grid>
</UserControl>

这是类代码。非常基本..来自ButtonBase并定义了一个DP。另请注意底部的集合类。

Imports System.Windows.Controls.Primitives
Imports System.Collections.ObjectModel

Public Class VerticalButton
    Inherits ButtonBase

    Public Property AllowedRoles() As String
        Get
            Return GetValue(AllowedRolesProperty)
        End Get
        Set(ByVal value As String)
            SetValue(AllowedRolesProperty, value)
        End Set
    End Property

    Public Shared ReadOnly AllowedRolesProperty As DependencyProperty = DependencyProperty.Register("AllowedRoles", GetType(String), GetType(VerticalButton), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnAllowedRolesChanged)))

    Public Shared Sub OnAllowedRolesChanged(ByVal d As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
        Dim sender As VerticalButton = CType(d, VerticalButton)
        sender.AllowedRoles = CStr(args.NewValue)
    End Sub

End Class

Public Class VerticalButtonCollection
    Inherits ObservableCollection(Of VerticalButton)

End Class

答案 3 :(得分:0)

通过执行以下操作结束此工作。其中一个技巧是你不能在模板绑定上使用转换器,因此你使用模板绑定绑定对象的DataContext,然后使用常规绑定来访问它。

<UserControl.Resources>
    <mt:AllowedRolesConverter x:Key="RolesConverter"></mt:AllowedRolesConverter>

    <Style x:Key="VerticalButton1Style" TargetType="mt:VerticalButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="mt:VerticalButton">
                    <Grid x:Name="RootGrid" DataContext="{TemplateBinding AllowedRoles}" Visibility="{Binding Converter={StaticResource RolesConverter}}">

                        <Border x:Name="border" BorderThickness="1">
                            <Border.BorderBrush>
                                <SolidColorBrush x:Name="borderBrush" Opacity="0" Color="Blue"/>
                            </Border.BorderBrush>
                            <ContentPresenter x:Name="VBContent"
                                              ContentTemplate="{TemplateBinding ContentTemplate}" 
                                              >

                            </ContentPresenter>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <mt:VerticalButtonCollection x:Key="TestCollection">
        <mt:VerticalButton AllowedRoles="Test1" 
                           Style="{StaticResource VerticalButton1Style}" 
                           Click="VerticalButton_Click"
                           >
            <TextBlock Text="Test Button"></TextBlock>
        </mt:VerticalButton>
        <mt:VerticalButton AllowedRoles="Test1" 
                           Style="{StaticResource VerticalButton1Style}" Click="VerticalButton_Click_1">
            <Ellipse Width="50" Height="50" Fill="Blue"></Ellipse>
        </mt:VerticalButton>
    </mt:VerticalButtonCollection>

    <Style x:Key="ListItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel x:Name="Root" Orientation="Horizontal">
                        <ContentPresenter Margin="5,5,5,5"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ListStyle" TargetType="ListBox">
        <Setter Property="ItemContainerStyle" Value="{StaticResource ListItemStyle}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <Grid x:Name="Root">
                        <ItemsPresenter></ItemsPresenter>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
    <ListBox Style="{StaticResource ListStyle}" ItemsSource="{StaticResource TestCollection}" />
</Grid>

    Public Class VerticalButton
    Inherits Button
    Implements INotifyPropertyChanged

    Public Property AllowedRoles() As String
        Get
            Return CStr(GetValue(AllowedRolesProperty))
        End Get
        Set(ByVal value As String)
            SetValue(AllowedRolesProperty, value)
        End Set
    End Property

    Public Shared ReadOnly AllowedRolesProperty As DependencyProperty = DependencyProperty.Register("AllowedRoles", GetType(String), GetType(VerticalButton), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnAllowedRolesChanged)))

    Public Shared Sub OnAllowedRolesChanged(ByVal d As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
        Dim sender As VerticalButton = CType(d, VerticalButton)
        sender.AllowedRoles = CStr(args.NewValue)
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

Public Class VerticalButtonCollection
    Inherits ObservableCollection(Of VerticalButton)

End Class