![](/img/trans.png)
[英]Datatrigger Binding to a property of the style's TargetType within a ControlTemplate and ContentPresenter
[英]Binding a ControlTemplate's Button's Content and Command Parameter Property
好的,这可能很简单,我做得不太正确。 我现在正在学习如何使用ItemsControl动态添加项目,如下所示。
<ItemsControl ItemsSource="{Binding Buttons}">
<ItemsControl.Template>
<ControlTemplate>
<Button FontWeight="Bold" Command="{Binding SelectMaterialCommand}" CommandParameter="{Binding CommandParameter}" Width="50" Height="50" Style="{StaticResource RoundedButtonStyle}" Margin="0,0,0,0" Click="Button_Click_2" Content="{Binding .Content}"></Button>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
ItemsControl ItemsSource属性绑定到Button的ObservableCollection。 在代码中,我可以创建一个按钮,设置Content和CommandParameter属性并将其添加到ObservableCollection。 当我运行该应用程序时,将填充一个按钮,但无法正确绑定Content和CommandParameter属性。
我尝试做几种不同的方法,例如Binding Path=.
, Binding Path=Content
, Binding Path=.Content
...等,但是似乎没有任何作用。 任何帮助,将不胜感激。
遵循Subramaniam B的建议,我通过利用位于UserControl.Resources部分中的数据模板来使它起作用。
<DataTemplate x:Key="temp" DataType="{x:Type local:ButtonModel}">
<telerik:RadButton FontWeight="Bold" FontSize="16" Command="{Binding ButtonCommand}" CommandParameter="{Binding Path=Content}" Width="100" Height="75" Margin="0,0,0,0" MouseLeftButtonUp="Button_Click_2" Content="{Binding Path=Content}">
<telerik:RadButton.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="GhostWhite" Offset="0"/>
<GradientStop Color="Gray" Offset="0.5"/>
<GradientStop Color="Gray" Offset="0.5"/>
<GradientStop Color="GhostWhite" Offset="1"/>
</LinearGradientBrush>
</telerik:RadButton.Background>
</telerik:RadButton>
</DataTemplate>
这是使用模板的地方:
<telerik:RadCarousel Loaded="MaterialCar_Loaded" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" x:Name="MaterialCar" Height="200" Background="Blue" ItemTemplate="{StaticResource temp}" ItemsSource="{Binding Buttons}" Margin="0,0,0,0"/>
在您的示例中,您将整个ItemsControl(又名:按钮列表)的模板设置为A按钮。
无论Buttons集合中有什么项目,此控件都将呈现为单个按钮。 而是,您需要保留控件本身的模板,而对列表中的项目使用I模板。
<ItemsControl ItemsSource="{Binding Buttons}">
<!-- ItemsControl.Template becomes ItemsControl.ItemTemplate below -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button FontWeight="Bold" Command="{Binding SelectMaterialCommand}" CommandParameter="{Binding CommandParameter}" Width="50" Height="50" Style="{StaticResource RoundedButtonStyle}" Margin="0,0,0,0" Click="Button_Click_2" Content="{Binding .Content}"></Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.