问题描述
大家好,
我的WPF视图中有一些按钮
I have some Buttons in my WPF View
[按钮1] [按钮2] [按钮3] ... [按钮100]
[Button 1] [Button 2] [Button 3] ... [Button 100]
我想基于元素列表{StatusName,StatusIsEnabled?}使用一种绑定启用/禁用
i want to enable/disable based on a list of elements {StatusName, StatusIsEnabled?} using a kind of Binding
示例:
列表
{(" BT01,True),(" BT02,False ), (" BT03,True)}
{("BT01,True),("BT02,False), ("BT03,True)}
视图
按钮名称= QUOT; BT01" IsEnabled = {Binding Condition(Button.Name = StatusName),Value =(StatusIsEnabled?)}
Button Name="BT01" IsEnabled={Binding Condition(Button.Name=StatusName), Value=(StatusIsEnabled?)}
按钮名称=" BT02" IsEnabled = {Binding Condition(Button.Name = StatusName),Value =(StatusIsEnabled?)}
Button Name="BT02" IsEnabled={Binding Condition(Button.Name=StatusName), Value=(StatusIsEnabled?)}
按钮名称=" BT03" IsEnabled = {Binding Condition(Button.Name = StatusName),Value =(StatusIsEnabled?)}
Button Name="BT03" IsEnabled={Binding Condition(Button.Name=StatusName), Value=(StatusIsEnabled?)}
推荐答案
根据你的描述,你想在你的窗口中创建新的Button,但你想绑定这些按钮IsEnable属性列表元素StatusEnabled属性?我是对的吗?
According to your description, you want to create new Button in your window, but you want to bind these buttons IsEnable property for list of element StatusEnabled property? Am I right?
如果是,我建议您可以使用ItemsCOntrol来执行此操作,使用UniformGrid设置列和行,我会做一个您可以查看的示例。
If yes, I suggest you can use ItemsCOntrol to do this, using UniformGrid to set column and row, I do one sample that you can take a look.
<ItemsControl ItemsSource="{Binding models}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" Rows="2" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button
Width="200"
Height="30"
Content="{Binding StatusName}"
IsEnabled="{Binding StatusIsEnabled}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public partial class Window8 : Window
{
public ObservableCollection<testviewmodel2> models { get; set; }
public Window8()
{
InitializeComponent();
models = new ObservableCollection<testviewmodel2>()
{
new testviewmodel2(){StatusName="BT01",StatusIsEnabled=true},
new testviewmodel2(){StatusName="BT02",StatusIsEnabled=false},
new testviewmodel2(){StatusName="BT03",StatusIsEnabled=true},
new testviewmodel2(){StatusName="BT04",StatusIsEnabled=false},
new testviewmodel2(){StatusName="BT05",StatusIsEnabled=false},
new testviewmodel2(){StatusName="BT06",StatusIsEnabled=true},
new testviewmodel2(){StatusName="BT07",StatusIsEnabled=false},
new testviewmodel2(){StatusName="BT08",StatusIsEnabled=true},
new testviewmodel2(){StatusName="BT09",StatusIsEnabled=false},
new testviewmodel2(){StatusName="BT010",StatusIsEnabled=true},
};
this.DataContext = this;
}
}
public class testviewmodel2
{
public string StatusName { get; set; }
public bool StatusIsEnabled { get; set; }
}
最好的问候,
Cherry
这篇关于绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!