模板化数据模板
我正在创建一个带有模板项(DataTemplate)的列表框。在此模板中,我希望能够根据与其绑定的数据来更改模板的一部分的呈现方式。这是我写的:
<ListBox Grid.Row="1"
ItemsSource="{Binding Indices}"
HorizontalContentAlignment="Stretch">
<DataTemplate>
<Grid Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- ****this does not work ;(**** -->
<ContentPresenter Grid.Column="0"
Content="{Binding}">
<ContentPresenter.ContentTemplateSelector>
<tmpl:BoolBasedSelector
Value="{Binding IsEditable, ElementName=MainCtrl}"
TrueTemplate="{StaticResource listBoxEditableLabel}"
FalseTemplate="{StaticResource listBoxNonEditableLabel}" />
</ContentPresenter.ContentTemplateSelector>
</ContentPresenter>
<!-- other stuff, not important right now -->
<Button Grid.Column="1" />
<Button Grid.Column="2" />
</Grid>
</DataTemplate>
</ListBox>
MainCtrl 是包含此列表的网格所在的主控件的名称。 如何使用可交换网格的元素编写ListBox的DataTamplate?谢谢你的建议。
编辑: tmpl:BoolBasedSelector非常简单:
class BoolBasedSelector: DataTemplateSelector {
public bool Value { get; set; }
public DataTemplate TrueTemplate { get; set; }
public DataTemplate FalseTemplate { get; set; }
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) {
return Value ? TrueTemplate : FalseTemplate;
}
}
引用的资源如下所示:
<DataTemplate x:Key="listBoxEditableLable">
<TextBox Text="{Binding Label}" />
</DataTemplate>
<DataTemplate x:Key="listBoxNonEditableLable">
<TextBlock Text="{Binding Label}" />
</DataTemplate>
ListBox绑定的DataContext的Indices属性:
interface IIndex {
string Label { get; set; }
IIndexValueProvider ValueProvider { get; set; }
IIndexValidator Validator { get; set; }
bool IsEditable { get; set; }
bool IsGrouped { get; set; }
}
I'm creating a ListBox with templated items (DataTemplate). Inside this template I want to have the possibility of changing the way a part of the template is presented depending of the data bounded to it. Here's what I wrote:
<ListBox Grid.Row="1"
ItemsSource="{Binding Indices}"
HorizontalContentAlignment="Stretch">
<DataTemplate>
<Grid Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<!-- ****this does not work ;(**** -->
<ContentPresenter Grid.Column="0"
Content="{Binding}">
<ContentPresenter.ContentTemplateSelector>
<tmpl:BoolBasedSelector
Value="{Binding IsEditable, ElementName=MainCtrl}"
TrueTemplate="{StaticResource listBoxEditableLabel}"
FalseTemplate="{StaticResource listBoxNonEditableLabel}" />
</ContentPresenter.ContentTemplateSelector>
</ContentPresenter>
<!-- other stuff, not important right now -->
<Button Grid.Column="1" />
<Button Grid.Column="2" />
</Grid>
</DataTemplate>
</ListBox>
MainCtrl is the name of the main control within which the grid containing this list is placed.
How do I write ListBox's DataTamplate with exchangeable grid's element? Thanks for you advices.
EDIT:
tmpl:BoolBasedSelector is really simple:
class BoolBasedSelector: DataTemplateSelector {
public bool Value { get; set; }
public DataTemplate TrueTemplate { get; set; }
public DataTemplate FalseTemplate { get; set; }
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) {
return Value ? TrueTemplate : FalseTemplate;
}
}
The referenced resources are like this:
<DataTemplate x:Key="listBoxEditableLable">
<TextBox Text="{Binding Label}" />
</DataTemplate>
<DataTemplate x:Key="listBoxNonEditableLable">
<TextBlock Text="{Binding Label}" />
</DataTemplate>
Indices property of the DataContext to which ListBox binds:
interface IIndex {
string Label { get; set; }
IIndexValueProvider ValueProvider { get; set; }
IIndexValidator Validator { get; set; }
bool IsEditable { get; set; }
bool IsGrouped { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模板使用静态资源名称“listBoxEditableLable”和“listBoxNonEditableLabel”,但您的数据模板键是“listBoxEditableLable”和“listBoxNonEditableLable”。
注意标签的不同拼写。 “标签”与“标签”
Your template uses the static resource names "listBoxEditableLable" and "listBoxNonEditableLabel", but your data template keys are "listBoxEditableLable" and "listBoxNonEditableLable".
Note the different spellings of label. "Label" vs "Lable"