有没有办法构建一个DataTemplate而不使用已弃用的 FrameworkElementFactory
或XamlReader.Load
字符串解释方法来编码(相关问题)或(另一个相关的)?
DataTemplate dt = new DataTemplate();
StackPanel sp = new StackPanel();
ComboBox cellComboBox = new ComboBox() { Visibility = Visibility.Collapsed };
CheckBox cellCheckBox = new CheckBox() { Visibility = Visibility.Collapsed };
sp.Children.Add(cellComboBox);
sp.Children.Add(cellCheckBox);
// then add the stackpanel somehow?
Run Code Online (Sandbox Code Playgroud)
更新:为什么?
这适用于可能需要多年支持的新程序.XamlReader字符串在运行时解析有一些构造函数,方法,属性......感觉很糟糕.在FrameworkElementFactory
已弃用了几个版本,但从来没有一个真正的替代品.
我试图加载Grid
与UIElement
S和/或FrameworkElement
该含有S Style
和DataTrigger
秒。
以下是一个简单的本机XAML,它可以按预期工作;在3个状态之间切换复选框可为矩形提供三种不同的样式(请参见底部的图像)。
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:XamlVsLoad"
mc:Ignorable="d"
Height="450" Width="800">
<CheckBox Name="MyCheck" IsChecked="True" Content="Checkbox" IsThreeState="True" Margin="10,10,0,0" Width="200"/>
<Rectangle Height="100" Width="100" StrokeThickness="5" Margin="10,50,100,100">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="White"/>
<Setter Property="Stroke" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyCheck, Path=IsChecked}" Value="True" >
<Setter Property="Fill" Value="Orange"/>
<Setter Property="Stroke" Value="Blue"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MyCheck, Path=IsChecked}" Value="False" >
<Setter Property="Fill" Value="Blue"/>
<Setter Property="Stroke" Value="Orange"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Grid>
Run Code Online (Sandbox Code Playgroud)
它也可以当我相同的文本保存到一个文件和动态加载到Window
的 Viewbox
与XAML这样S:
<Grid.ColumnDefinitions> …
Run Code Online (Sandbox Code Playgroud) 下面的c ++/cli模板正在运行,但似乎应该有一种方法可以进一步概括模板或添加一个可以在编译时创建模板实例的帮助器.
想到像http://en.cppreference.com/w/cpp/utility/integer_sequence这样的东西可能有用,但需要帮助/实施者功能的一些帮助.
简化主要内容以演示所需语法与当前使用的语法:
int main(array<String^>^ args) {
// the actual number of possible char lengths is sparse (generally)
// but the API allows for 1-1024
List<int>^ varList = gcnew List<int>();
varList->Add(40);
varList->Add(80);
varList->Add(128);
SortedList<int, List<String^>^>^ allStrings = gcnew SortedList<int, List<String^>^>();
// want something like this, but the compiler complains that
// the template is invalid expectes compile-time constant expressions
for each(int key in varList) {
allStrings->Add(key, UpdateTest<key>());
}
// this works, but has 1024 lines of case …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个WPF应用程序,其中我有一个显示帐户列表的StackPanel.每个帐户的代码后面都有一个Account对象,存储在List结构中.我想将此数据绑定到我的WPF,但我不想要一个列表框.
相反,我为每个帐户摘要的显示方式定义了一个模板.然后我想将它们堆叠在StackPanel中并将其称为一天.
问题是数据绑定只从列表中获取第一个项目,就是这样.如何绑定它,以便有效地创建一个很好的格式化数据块的堆栈列表?
以下是相关的WPF代码:
<StackPanel Name="sp_AccountList" Margin="0,0,0,0" VerticalAlignment="Top">
<StackPanel.Resources>
<svc:AccountBalanceColorConverter x:Key="accountColorConverter" />
<Style x:Key="AccountSummaryBackgroundGradient" TargetType="{x:Type StackPanel}">
<!-- nice formatting code here -->
</Style>
<Style x:Key="AccountSummaryNameStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="10,0,0,0" />
<Setter Property="FontSize" Value="18" />
<Setter Property="Height" Value="20" />
<Setter Property="FontFamily" Value="Cambria" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style x:Key="AccountSummaryBalanceStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="10,0,0,0" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Height" Value="20" />
<Setter Property="FontFamily" Value="Cambria" />
<Setter Property="Background" Value="Transparent" />
</Style>
<ObjectDataProvider x:Key="accounts"
ObjectType="{x:Type …
Run Code Online (Sandbox Code Playgroud) wpf ×3
c# ×2
datatemplate ×2
c++ ×1
c++-cli ×1
clone ×1
data-binding ×1
templates ×1
uielement ×1
xaml ×1