我有这个模板:
<DataTemplate x:Key="currencyColumn">
<StackPanel Height="210" Margin="0,0,0,0" VerticalAlignment="Top" Width="64" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<Image Height="64" Source="assets/images/chaos.png"/>
<TextBox x:Name="total" Height="24" TextWrapping="Wrap" Text="{Binding Total}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="48" FontSize="16" FontWeight="Bold" Margin="0,10,0,0" IsReadOnly="True"/>
<TextBox x:Name="used" Height="24" TextWrapping="Wrap" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="48" FontWeight="Bold" FontSize="16" Margin="0,10,0,0"/>
<TextBox x:Name="payed" Height="24" TextWrapping="Wrap" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="48" FontWeight="Bold" FontSize="16" Margin="0,10,0,0"/>
<TextBox x:Name="owed" Height="24" TextWrapping="Wrap" Text="{Binding Owed}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="48" FontWeight="Bold" FontSize="16" Margin="0,10,0,0" IsReadOnly="True"/>
</StackPanel>
</DataTemplate>
我想做的是在ListBox
通过runtime
添加codebehind
未定义次数的此模板,每个模板都修改了图像的来源,我希望将textbox
与readonly
标记绑定到某些变量。
答案 0 :(得分:1)
当你只能直接在View(xaml)中执行它时,我没有看到通过代码绑定它的意义。
我想你有一个特定的模型可以在你的UI中显示这些数据:
public class MyModel
{
public string ImageSource {get;set;}
public double Total {get;set;}
public double Used {get;set;}
public double Paid {get;set;}
public double Owed {get;set;}
}
在你的虚拟机中,你会有一个这样的集合,因为你说你将有一个未定义的数量。
using Prism.Mvvm;
public class MyVM : BindableBase
{
public ObservableCollection<MyModel> MyModelCollection
{
get {return _myModelCollection;}
set {SetProperty(ref _myModelCollection);}
}
private ObservableCollection<MyModel> _myModelCollection;
}
最后,在你的视图(xaml)
中<ItemsControl ItemsSource="{Binding MyModelCollection, Mode=OneWay}"
ItemTemplate="{StaticResource currencyColumn}">
</ItemsControl>
我在这里使用您已定义的DataTemplate。