我正在制作自己的 WPF 用戶控制元件,以便為用戶提供選擇資料的選項。我有一個 Combobox,它的樣式位于單獨的資源字典中。如果組合框的 SelectedIndex 設定為 0,我想折疊文本框。
這是我的代碼:
UserControl x:Class="Baileys.CustomChartControl"
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"
xmlns:local="clr-namespace:Baileys"
mc:Ignorable="d"
d:DesignHeight="81.855" Loaded ="UserControl_Loaded" MouseDoubleClick="UserControl_DoubleClick" MouseDown="UserControl_MouseDown" Width="759.405" >
<Grid x:Name="grid" Background="Transparent" Margin="0,0,-368,-23"> `
<ComboBox HorizontalAlignment="Left" Height="100" Margin="173,99,0,-123"VerticalAlignment="Top" Style="{DynamicResource CBstyle}" Width="120"/>
<TextBlock x:Name="MyCoursesTxt" Text="{Binding MyCourses}" />
</Grid>`
我使用 Microsoft blend 來制作我的觸發器,但是它沒有給我一個選項來在我的新用戶控制元件中設定一個屬性基本觸發器。
uj5u.com熱心網友回復:
使用事件SelectionChanged來控制組件是否顯示更直接。
我寫了一些演示代碼,希望它有幫助:
主視窗.xaml
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<StackPanel>
<ComboBox x:Name="MyComboBox" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="Test1"></ComboBoxItem>
<ComboBoxItem Content="Test2"></ComboBoxItem>
</ComboBox>
<TextBlock x:Name="MyCoursesTxt" Text="This is TextBlock." />
</StackPanel>
</Window>
主視窗.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace TestWpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.MyComboBox.SelectedIndex == 0)
{
this.MyCoursesTxt.Visibility = Visibility.Collapsed;
}
else
{
this.MyCoursesTxt.Visibility = Visibility.Visible;
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/381185.html
上一篇:Grid中的ListView有沒有辦法繼承網格的ColumnDefinitions和RowDefinitions?
