我尝试动态更改文本框和变形框之间的可见/折叠(如此Dynamically Changing Combobox to TextBox in datagrid inside Cell Editing Template in silverlight 4),但是当我在“Silverlight业务应用程序”中使用此解决方案时,似乎存在问题,TextBox (在DataGridTemplateColumn中,Header =“Destination”在进入编辑模式时拒绝显示。
我已经将它缩小到MainPage.xaml中存在某些控件(例如hyperlinkbutton或contentcontrol)时出现问题,如果我删除它们就可以正常工作了。
Link to example project, BusinessApplication8
任何人?
MainPage.xaml中
<Grid x:Name="LayoutRoot">
<Border x:Name="ContentBorder" >
<navigation:Frame x:Name="ContentFrame" Source="/Home" >
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
</Border>
<Grid >
<Grid x:Name="NavigationGrid">
<Border x:Name="BrandingBorder">
<StackPanel x:Name="BrandingStackPanel" >
<ContentControl />
<TextBlock x:Name="ApplicationNameTextBlock" Text="ApplicationName"/>
</StackPanel>
</Border>
<Border x:Name="LinksBorder">
<StackPanel x:Name="LinksStackPanel">
<HyperlinkButton x:Name="Link1" NavigateUri="/Home" TargetName="ContentFrame" Content="Navigate Home"/>
<Rectangle x:Name="Divider1" />
<HyperlinkButton x:Name="Link2" NavigateUri="/About" TargetName="ContentFrame" Content="Navigate About"/>
</StackPanel>
</Border>
</Grid>
</Grid>
</Grid>
HomePage.xaml
<Grid x:Name="LayoutRoot">
<StackPanel x:Name="ContentStackPanel">
<TextBlock x:Name="HeaderText"
Text="Header text"/>
<sdk:DataGrid x:Name="dataGrid1" AutoGenerateColumns="False"
PreparingCellForEdit="dataGrid1_PreparingCellForEdit">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Instruction Type">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding operationType}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox x:Name="instruction" SelectedValue="{Binding operationType, Mode=TwoWay}">
<s:String>TextBox</s:String>
<s:String>ComboBox</s:String>
<s:String>ListBox</s:String>
</ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Destination">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="2" Text="{Binding destination}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<ListBox x:Name="listBox" SelectedValue="{Binding destination, Mode=TwoWay}">
<s:String>azerty</s:String>
<s:String>qsdfgh</s:String>
</ListBox>
<TextBox x:Name="textBox" Text="{Binding destination, Mode=TwoWay}" />
<ComboBox x:Name="comboBox" SelectedValue="{Binding destination, Mode=TwoWay}">
<s:String>azerty</s:String>
<s:String>qsdfgh</s:String>
</ComboBox>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
</Grid>
Home.xaml.cs
public Home()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
dataGrid1.ItemsSource = new List<Instruction>()
{
new Instruction(){operationType = "TextBox", destination ="azerty"},
new Instruction(){operationType = "ComboBox", destination = "qsdfgh"},
new Instruction(){operationType = "ComboBox", destination = "wxcvwxcv"}
};
}
private void dataGrid1_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
if ((string)e.Column.Header == "Destination")
{
var textBox = e.EditingElement.FindName("textBox") as TextBox;
var comboBox = e.EditingElement.FindName("comboBox") as ComboBox;
var listBox = e.EditingElement.FindName("listBox") as ListBox;
var instruction = e.EditingElement.DataContext as Instruction;
if (instruction.operationType == "ComboBox")
{
textBox.Visibility = System.Windows.Visibility.Collapsed;
comboBox.Visibility = System.Windows.Visibility.Visible;
listBox.Visibility = System.Windows.Visibility.Collapsed;
}
else if (instruction.operationType == "ListBox")
{
//tb.DataContext = null;
//cb.DataContext = e.EditingElement.DataContext;
textBox.Visibility = System.Windows.Visibility.Collapsed;
comboBox.Visibility = System.Windows.Visibility.Collapsed;
listBox.Visibility = System.Windows.Visibility.Visible;
}
else if (instruction.operationType == "TextBox")
{
//tb.DataContext = null;
//cb.DataContext = e.EditingElement.DataContext;
textBox.Visibility = System.Windows.Visibility.Visible;
comboBox.Visibility = System.Windows.Visibility.Collapsed;
listBox.Visibility = System.Windows.Visibility.Collapsed;
}
}
}
课堂教学
public class Instruction
{
public string operationType { get; set; }
public string destination { get; set; }
}
当我删除HyperlinkButtons“Link1”,“Link2”和MainPage.xaml中的ContentControl时,它似乎有效。