如何将特定 ObservableCollection 项的属性绑定到 CustomControl 的 ControlTemplate
我有一个带有可观察的“状态”集合的自定义控件,有点像多状态按钮。这里仅提供必要的代码以避免混淆:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
RelativeSource
绑定Try using a
RelativeSource
binding