C#WPF动态添加标签但加载xaml文件会清除单选按钮,为什么?

时间:2010-12-25 02:13:44

标签: wpf xaml tabitem

所以我有一个包含TabControl作为主要UI元素的窗口。我默认在xaml编辑器中添加两个选项卡。当程序运行时,我想添加具有相同设置的新选项卡(但与前两个选项卡不同)。我可以通过致电:

来做到这一点
URLTabContentControl documentRoot = (URLTabContentControl)Application.LoadComponent(new Uri(@"URLTabContentControl.xaml", UriKind.Relative));

这会加载我在xaml文件中定义的控件。基本上这就是我要在我将要添加的新选项卡中显示的内容。此时我打电话给:

TabItem ti = new TabItem();
ti.Header = "*New Capture URL " + (mainTabControl.Items.Count-1);
ti.Content = documentRoot;
int i = mainTabControl.Items.Add(ti);
mainTabControl.SelectedIndex = i;

创建新的TabItem会添加一个标头和通过xaml文件创建的控件。问题是xaml文件有几个不同的单选按钮组,每次添加新选项卡时,现有选项卡上的单选按钮都将被取消选中。我不认为这是我的代码中的错误,所以我想知道是否有人可以指出我正确的方向。没有其他类型的控件像这样搞砸了他们的状态。

这是成为TabItems内容的xaml:

<UserControl x:Class="CustomCamApplication.URLTabContentControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="340" d:DesignWidth="634">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="45*" />
        <RowDefinition Height="42*" />
        <RowDefinition Height="41*" />
        <RowDefinition Height="31*" />
        <RowDefinition Height="34*" />
        <RowDefinition Height="29*" />
        <RowDefinition Height="118*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="101*" />
        <ColumnDefinition Width="105*" />
        <ColumnDefinition Width="65*" />
        <ColumnDefinition Width="45*" />
        <ColumnDefinition Width="30*" />
        <ColumnDefinition Width="88*" />
        <ColumnDefinition Width="200*" />
    </Grid.ColumnDefinitions>
    <Label Content="URL Suffix:" Height="28" HorizontalAlignment="Right" Margin="0,12,0,0" Name="urlSuffixLabel" VerticalAlignment="Top" ToolTip="Enter the part of the path that will be after the domain. http://my.domain.com:port/(enter this part)" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="6,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="401" Grid.Column="1" Grid.ColumnSpan="6" />
    <Label Content="Video Source:" Height="28" HorizontalAlignment="Right" Margin="0,10,0,0" Name="videoSourceLabel" VerticalAlignment="Top" Grid.Row="1" ToolTip="Select the video source you want to use for this URL." />
    <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="6,10,0,0" Name="videoSourceComboBox" VerticalAlignment="Top" Width="401" Grid.Row="1" Grid.ColumnSpan="6" SelectionChanged="videoSourceComboBox_SelectionChanged" />
    <Label Content="Captured Image" Grid.Row="2" Height="28" HorizontalAlignment="Right" Margin="0,10,0,0" Name="capturedImageLabel" VerticalAlignment="Top" Width="101" />
    <Label Content="Filename Prefix:" Grid.Column="1" Grid.Row="2" Height="28" HorizontalAlignment="Right" Margin="0,10,0,0" Name="filenamePrefixLabel" VerticalAlignment="Top" Width="95" ToolTip="Enter the front part of the file names used for capture file by this video source. Example prefix1002.png" />
    <TextBox Grid.Column="2" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="6,12,0,0" Name="filenamePrefixTextBox" VerticalAlignment="Top" Width="120" Text="capture" Grid.ColumnSpan="3" />
    <Label Content="Capture Type:" Grid.Row="5" Height="28" HorizontalAlignment="Right" Name="captureTypeLabel" VerticalAlignment="Top" Width="83" ToolTip="Store only a single image or store a series of images on the drive." />
    <RadioButton Content="Single Image" Grid.Column="1" Grid.Row="5" Height="16" HorizontalAlignment="Left" Margin="6,5,0,0" Name="captureTypeSingleRadioButton" VerticalAlignment="Top" GroupName="captureType" IsChecked="True" />
    <RadioButton Content="Series" Grid.Column="2" Grid.Row="5" Height="16" HorizontalAlignment="Left" Margin="6,5,0,0" Name="captureTypeSeriesRadioButton" VerticalAlignment="Top" GroupName="captureType" />
    <Label Content="File Format:" Grid.Column="1" Grid.Row="3" Height="28" HorizontalAlignment="Right" Margin="0,3,0,0" Name="captureFormatLabel" VerticalAlignment="Top" Width="73" ToolTip="Select the format of the stored capture files." />
    <RadioButton Content="PNG" Grid.Column="2" Grid.Row="3" Height="16" HorizontalAlignment="Left" Margin="6,8,0,0" Name="fileFormatPNGRadioButton" VerticalAlignment="Top" GroupName="fileFormat" IsChecked="True" />
    <RadioButton Content="JPG" Grid.Column="3" Grid.Row="3" Height="16" HorizontalAlignment="Left" Margin="0,8,0,0" Name="fileFormatJPGRadioButton" VerticalAlignment="Top" GroupName="fileFormat" />
    <RadioButton Content="BMP" Grid.Column="4" Grid.Row="3" Height="16" HorizontalAlignment="Left" Margin="8,8,0,0" Name="fileFormatBMPRadioButton" VerticalAlignment="Top" GroupName="fileFormat" Grid.ColumnSpan="2" />
    <CheckBox Content="Zero Pad Image Series Names" Grid.Column="5" Grid.Row="2" Height="16" HorizontalAlignment="Left" Margin="9,15,0,0" Name="filenameZeroPadCheckBox" VerticalAlignment="Top" ToolTip="Example prefix00001.png" Grid.ColumnSpan="2" />
    <Label Content="Dimensions:" Grid.Column="1" Grid.Row="4" Height="28" HorizontalAlignment="Right" Margin="0,6,0,0" Name="captureDimensionsLabel" VerticalAlignment="Top" Width="73" />
    <RadioButton Content="Source Default" Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="4" Height="16" HorizontalAlignment="Left" Margin="6,11,0,0" Name="captureDimensionsDefaultRadioButton" VerticalAlignment="Top" Width="94" GroupName="captureDimensions" IsChecked="True" ToolTip="The default capture size for the selected video source." Checked="captureDimensionsDefaultRadioButton_Checked" />
    <Label Content="Capture Interval:" Grid.Row="6" Height="28" HorizontalAlignment="Right" Margin="0,5,0,0" Name="captureIntervalLabel" VerticalAlignment="Top" Width="101" ToolTip="Enter the number of seconds between frame captures." />
    <TextBox Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="6" Height="23" HorizontalAlignment="Left" Margin="6,7,0,0" Name="catureIntervalTextBox" VerticalAlignment="Top" Width="120" Text="5" PreviewTextInput="captureIntervalTextBox_PreviewTextInput"  DataObject.Pasting="captureIntervalTextBox_Pasting"/>
    <RadioButton Content="Reported Mode" Grid.Column="4" Grid.ColumnSpan="2" Grid.Row="4" Height="16" HorizontalAlignment="Left" Margin="8,11,0,0" Name="captureDimensionsSourceReportedRadioButton" VerticalAlignment="Top" GroupName="captureDimensions" ToolTip="Select a mode from one reported by the device." Checked="captureDimensionsSourceReportedRadioButton_Checked" />
    <ComboBox Grid.Column="6" Grid.Row="4" Height="23" HorizontalAlignment="Left" Margin="0,6,0,0" Name="reportedModeComboBox" VerticalAlignment="Top" Width="174" IsEnabled="False" />
</Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

假设您的URLTabContentControl控件上有3组单选按钮,名为Group1,Group2和Group3。当您基于TabItem动态创建URLTabContentControl时,该实例会有另一组单选按钮,这些按钮已经扩展了您的初始组定义。这3组不是每个实例本身都是孤立的。

测试此理论的一种方法是在URLTabContentControl控件上留下一组单选按钮,并删除组名称,让框架处理默认行为;它应该按预期工作,因为URLTabContentControl

上的单选按钮没有明确的组名