C# WPF 通过加载 xaml 文件动态添加选项卡会清除单选按钮,为什么?
所以我有一个包含 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>
So I have a window that contains a TabControl as the main UI element. I add two tabs by default in the xaml editor. When the program runs I want to add new tabs that have identical setups (but are different than the first two tabs). I can do this by calling:
URLTabContentControl documentRoot = (URLTabContentControl)Application.LoadComponent(new Uri(@"URLTabContentControl.xaml", UriKind.Relative));
Which loads up a control I have defined in a xaml file. Basically this is what I want to show up inside of the new tab I will add. At this point I call:
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;
Which creates the new TabItem adds a header and the control that was create via the xaml file. The issue is the xaml file has several different radio button groups and everytime I add a new tab the radio buttons on the existing tabs all become unselected. I don't think this is a bug in my code so I'm wondering if anyone can point me in the right direction. None of the other types of controls have their state screwed up like this.
This is the xaml that becomes the content of the TabItems:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的
URLTabContentControl
控件上有 3 组单选按钮,标题为 Group1、Group2 和 Group3。当您基于URLTabContentControl
动态创建TabItem
时,该实例就会拥有另一组单选按钮,这些单选按钮已在您的初始组定义上进行了扩展。这 3 个组本身并不孤立于每个实例。测试这一理论的一种方法是在
URLTabContentControl
控件上保留一组单选按钮,并删除组名称并让框架处理默认行为;它现在应该按预期工作,因为URLTabContentControl
上的单选按钮没有绑定明确的组名称Let's assume you have 3 groups of radio buttons on your
URLTabContentControl
control titled Group1, Group2, and Group3. When you dynamically create aTabItem
based onURLTabContentControl
that instance then has yet another group of radio buttons that have since expanded on your initial group definitions. Those 3 groups are not isolated to each instance per se.One way to test this theory is to leave a single grouping of radio buttons on your
URLTabContentControl
control and remove the group name and let the framework handle the default behavior; it should work as expected now since there is no explicit group name tied to the radio buttons onURLTabContentControl