如何将特定 ObservableCollection 项的属性绑定到 CustomControl 的 ControlTemplate

发布于 2024-12-14 10:55:43 字数 1762 浏览 0 评论 0原文

我有一个带有可观察的“状态”集合的自定义控件,有点像多状态按钮。这里仅提供必要的代码以避免混淆:

public class MyMultiStateBtn : ItemsControl
{
    MyMultiStateBtn()
    {
       m_states = new ObservableCollection<MyState>();
    }

    private ObservableCollection<MyState> m_states;
    public System.Collections.ObjectModel.ObservableCollection<MyState> States
    {
       get { return m_states; }
       set { m_states = value; }
    }
}

“MyState”类(集合中的对象)包含一个公共“Name”属性,我希望将其显示在每个自定义控件的位置上方。

现在。在窗口的 XAML 中,我创建了 2 个 MyMultiStateBtn 实例,其中之一如下所示(显然,第二个实例具有不同的“状态”列表):

<local:MyMultiStateBtn x:Name="AAA" Template="{StaticResource MultiStateBtnTpl}">
    <local:MyMultiStateBtn.States>
        <local:MyState Name="On"/>
        <local:MyState Name="Off" Value="1"/>
        <local:MyState Name="Auto" Value="2"/>
    </local:MyMultiStateBtn.States>
</local:MyMultiStateBtn>

到目前为止一切顺利。请注意,这些控件使用模板(“MultiStateBtnTpl”)。该模板使用文本框来显示州名称...这就是我在语法中迷失的地方。我将免除您数百万次失败的尝试...这大概是我的想法(注意:我知道这种绑定不起作用!):

<ControlTemplate x:Key="MultiStateBtnTpl" TargetType="{x:Type loca:MyMultiStateBtn}">
    <Grid Width="130" Height="120">
        <TextBlock x:Name="tkValue1" Text="{Binding States, Path=[0].Name}" />
        <TextBlock x:Name="tkValue2" Text="{Binding States, Path=[1].Name}" />
        <TextBlock x:Name="tkValue3" Text="{Binding States, Path=[2].Name}" />
    </Grid>
</ControlTemplate>

简而言之:如何使模板找到埋在项目中的字符串可观察集合的...

我知道信息就在那里,但我发现没有任何东西可以帮助我访问数组(集合)的各个元素中的数据。

预先感谢您的任何帮助!

Seb

PS:我可以创建 3 个单独的属性,并在控件模板中访问这些属性。然而,在某些情况下,可能有多达 10 个甚至 20 个位置。我想避免拥有 20 个独立的属性。

I have a custom control with an observable-collection of "states", sort of like a multiple-state-button. Only the necessary code is here to avoid confusion:

public class MyMultiStateBtn : ItemsControl
{
    MyMultiStateBtn()
    {
       m_states = new ObservableCollection<MyState>();
    }

    private ObservableCollection<MyState> m_states;
    public System.Collections.ObjectModel.ObservableCollection<MyState> States
    {
       get { return m_states; }
       set { m_states = value; }
    }
}

The "MyState" class (the objets in the collection) contains a public "Name" property that I want to display above each my custom control's positions.

Now. In the window's XAML, I created 2 instances of MyMultiStateBtn, where one of them looks like this (the second one has different list of "states", obviously):

<local:MyMultiStateBtn x:Name="AAA" Template="{StaticResource MultiStateBtnTpl}">
    <local:MyMultiStateBtn.States>
        <local:MyState Name="On"/>
        <local:MyState Name="Off" Value="1"/>
        <local:MyState Name="Auto" Value="2"/>
    </local:MyMultiStateBtn.States>
</local:MyMultiStateBtn>

So far so good. Notice these controls are using a template ("MultiStateBtnTpl"). This template uses TextBoxes to display the state names... and that's where I got lost in the syntax. I'll spare you my million-failed attempts... here's roughly what I had in mind (note: I know this binding does NOT work!):

<ControlTemplate x:Key="MultiStateBtnTpl" TargetType="{x:Type loca:MyMultiStateBtn}">
    <Grid Width="130" Height="120">
        <TextBlock x:Name="tkValue1" Text="{Binding States, Path=[0].Name}" />
        <TextBlock x:Name="tkValue2" Text="{Binding States, Path=[1].Name}" />
        <TextBlock x:Name="tkValue3" Text="{Binding States, Path=[2].Name}" />
    </Grid>
</ControlTemplate>

In a nutshell: How can I make the template find the strings buried within the items of an observable collection...

I know the information is out there, but nothing I found helped me access data within individual elements of an array (collection).

Thanks in advance for any help!

Seb

PS: I could create 3 seperate properties, and access those in the controltemplate. However, in some cases there could be up to 10, or even 20 positions. I want to avoid having 20 separate properties.

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

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

发布评论

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

评论(1

空名 2024-12-21 10:55:43

尝试使用 RelativeSource 绑定

<TextBlock x:Name="RelativeSourceBinding" 
           Text="{Binding States[0].Name, 
               RelativeSource={AncestorType local:MyMultiStateBtn}}" />

Try using a RelativeSource binding

<TextBlock x:Name="RelativeSourceBinding" 
           Text="{Binding States[0].Name, 
               RelativeSource={AncestorType local:MyMultiStateBtn}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文