xaml 相关问题

可扩展应用程序标记语言(XAML)是一种基于XML的声明式语言,用于在各种框架中初始化结构化值和对象。当问题是关于具有特定框架的XAML的使用时,还应该提供框架的标签,例如, [wpf](Windows Presentation Foundation),[silverlight],[windows-phone],[windows-store-apps](Windows 8商店应用),[win-universal-app],[xamarin.forms]或[工作流程 - 基础]

为什么 WPF ProgressBar PART_Indicator 添加不透明度?

我正在尝试为 WPF 中的 ProgressBar 控件制作自定义样式。但 PART_Indicator 正在降低栏右侧的不透明度。 (查看 25%、50%、75% 的示例) 当酒吧达到 100% 时...

回答 1 投票 0

错误 XFC0045 - 如何将 CarouselView 绑定到毛伊岛的 ObservableCollection?

我正在尝试将 ObservableCollection 绑定到 。在 CarouselView 中,我有一个 ,我试图将其绑定到 ObservableCollection 中的公共属性。嗬...

回答 1 投票 0

WinUI 3 ComboBox ItemsSource 绑定字典<string, CustomClassObject> 抛出 InvalidOperationException“目标类型不是投影类型”

描述错误 当 TValue 是类对象时,ComboBox 的 ItemsSource 与 Dictionary 绑定会引发错误。 我在我的项目中使用 WinUI 3 和 C#。 我看了很多其他的...

回答 1 投票 0

向超链接 C# 添加导航

我想在点击某个超链接时添加一个导航到另一个页面的过程。 我想在点击某个超链接时添加到另一个页面的导航过程。 <DataGridHyperlinkColumn x:Name="Hyperlink" Header="Hyperlink" ContentBinding="{Binding Name}" DataGridHyperLinkColumn 没有点击功能。那么如何实现“点击功能”呢? 预先感谢 在您的代码隐藏中,您可以尝试订阅超链接的 RequestNavigate 事件。 // In constructor or after component initialization. Hyperlink.RequestNavigate += Hyperlink_RequestNavigate; 在 Hyperlink_RequestNavigate 方法中,您可以处理导航逻辑。 private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) { // Navigate to the URL specified in the hyperlink Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); e.Handled = true; }

回答 1 投票 0

光标一直向左跳(到开头)

我正在尝试添加不同的行为和转换器。但无论我在做什么,光标都会回到开头。 例如,我正在尝试使用行为来阻止超过 1 个白人......

回答 1 投票 0

WPF 每个 TabItem 的初始焦点

好吧,我知道这个标题看起来到处都有答案,但这里找到的答案都不适合我。那么,就这样吧。 我有这样的布局: 好吧,我知道这个标题看起来到处都有答案,但这里找到的答案都不适合我。那么,就到这里吧。 我有这个布局: <Window> <Grid> <DockPanel> <TabControl> <TabItem> <Page x:Name="p"> <Grid x:Name="g2"> <TabControl x:Name="tc"> <TabItem x:Name="ti1"> <StackPanel x:Name="sp"> <C:TextBox x:Name="txt"/> </StackPanel> </TabItem> <TabItem x:Name="ti2"> <C:DataGrid x:Name="dg"/> </TabItem> </TabControl> </Grid> </Page> </TabItem> </TabControl> </DockPanel> </Grid> </Window> 现在,我的目标是当选择 txt TextBox 时将焦点放在 ti1 TabItem 上,并在选择 dg DataGrid 时将焦点放在 ti2 TabItem 上。另外,我真的很想将其设置为XAML。 注意:我只能使用此处命名的控件,所以直到 Page 控件。 到目前为止我尝试过什么: 在 FocusManager.FocusedElement="{Binding ElementName=txt}" txt 的父树中的所有父控件上设置 Control(直到 Page)。 在 FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" 和 txt 控件上设置 dg。 通过 TabControl 的 SelectionChanged 事件在代码中设置焦点: if ( ti1.IsSelected ) { tc.UpdateLayout(); FocusManager.SetFocusedElement( sp, txt ); } 和 if ( ti1.IsSelected ) { tc.UpdateLayout(); txt.焦点(); } TextBox和DataGrid控件的创建方式与UserControl类似,但实际上是继承TextBox和DataGrid的类,如下所示: <TextBox ... </TextBox> 和 public partial class TextBox : System.Windows.Controls.TextBox 正如我所说,XAML解决方案是理想的,但如果前者不可能的话,我也会选择代码一。 好吧,Keyur PATEL 的答案中的调度程序部分对我来说是解决方案,尽管不是完整的解决方案。我的答案是用 TabControl 更新 Dispatcher 布局,然后调用 Focus Dispatcher。所以,对我来说完整的答案是: Dispatcher.BeginInvoke( (Action) (() => tc.UpdateLayout()) ); Dispatcher.BeginInvoke( (Action) (() => txt.Focus() ) ); 或者您可以只使用 Invoke 来代替,让 UI 线程等待您的 Action。 之所以我必须使用Dispatcher,是因为我首先用它来更改选定的选项卡。至少这是我最好的猜测。 您可以尝试类似于代码隐藏解决方案的方法,但有以下变体: <TabControl x:Name="tc" SelectionChanged="tc_selectionChanged"> 并在后面的代码中: InitializeComponent(); //if you know which control to focus by default when page is first loaded Dispatcher.BeginInvoke(new Action(() => { txt.Focus(); })); 和 private void tc_selectionChanged(object sender, SelectionChangedEventArgs e) { if (ti1.IsSelected) { txt.Focus(); } else if (ti2.IsSelected) { dg.Focus(); } } 我在自己的 WPF 应用程序上尝试了这个确切的设置,所以我知道它有效。 有用的链接: WPF TabControl 在 SelectionChanged 上,将焦点设置到文本字段 和 如何在 WPF 中的 tabItem 中集中控制(尽管对我来说,它不需要 UpdateLayout()) 找到使用扩展属性的解决方案 public static class TabItemExtention { public static bool GetIsSelected(DependencyObject obj) { return (bool)obj.GetValue(IsSelectedProperty); } public static void SetIsSelected(DependencyObject obj, bool value) { obj.SetValue(IsSelectedProperty, value); } public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached( "IsSelected", typeof(bool), typeof(TabItemExtention), new UIPropertyMetadata(false, OnIsSelectedPropertyChanged)); private static void OnIsSelectedPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var uie = (UIElement)d; var tabItem = uie as TabItem; if (tabItem != null) tabItem.IsSelected = (bool)e.NewValue; if ((bool)e.NewValue) { uie.UpdateLayout(); } } } XAML: ctrl:TabItemExtention.IsSelected="{Binding IsTabNewCustomsDelaySelected}" 对我有用的是扩展方法。有些东西阻止我的元素获得焦点,所以等到上下文空闲修复了这个问题。 public static class UIElementExtensions { public static void FocusOnIdle(this UIElement element) { var result = element.Focus(); if (result == false) { Application.Current.Dispatcher.Invoke(new Action(() => { element.Focus(); }), Threading.DispatcherPriority.ContextIdle); } } }

回答 4 投票 0

Visual Studio 2022 .Net MAUI 项目智能感知不适用于某些 XAML 文件

我有一个非常简单的.Net MAUI 项目,包含三个(XAML)页面。该项目编译并运行,没有任何错误或警告。 当我编辑其中一个 XAML 页面并键入 < character, the follo...

回答 2 投票 0

从外部文件在自己的运行空间中显示 wpf 对话框

我有这个概念验证的 powershell 脚本,它将以其当前的形式工作,并将显示一个空白对话框,但以 wpf-ui 库为主题。 如果我更改 $reader = (New-Object System.Xml.XmlNode...

回答 1 投票 0

MVVM - 当绑定属性不存在时隐藏控件

我想知道如果视图模型中不存在控件绑定的属性,是否可以隐藏视图上的控件。例如,如果我有以下内容: 我想知道如果视图模型中不存在控件绑定的属性,是否可以隐藏视图上的控件。例如,如果我有以下内容: <CheckBox Content="Quote" IsChecked="{Binding Path=IsQuoted}" /> 我可以在 XAML 中检测到视图模型上不存在 IsQuoted 属性,并简单地隐藏该实例中的控件吗? 我本质上是创建一个向导对话框,该对话框在视图模型集合中移动,显示每个视图模型的关联视图。对于集合中的某些视图模型,将存在“IsQuoted”属性,而对于某些视图模型则不存在。 我希望在这些视图之外有一个复选框,当当前视图模型具有该属性时显示,当视图模型不具有该属性时隐藏。所有视图模型都派生自一个公共基类,但我不想通过添加“ShowQuoted”属性等来弄乱基类。 想法?并且,预先感谢... 使用始终返回 Visibility.Visible 的转换器来处理该值存在的情况。通过指定回退值来处理该值不存在的情况。当该属性不存在时,绑定失败并接收后备值。 <Page.DataContext> <Samples:OptionalPropertyViewModel/> </Page.DataContext> <Grid> <Grid.Resources> <Samples:AlwaysVisibleConverter x:Key="AlwaysVisibleConverter" /> </Grid.Resources> <CheckBox Content="Is quoted" IsChecked="{Binding IsQuoted}" Visibility="{Binding IsQuoted, Converter={StaticResource AlwaysVisibleConverter}, FallbackValue=Collapsed}" /> </Grid> public class OptionalPropertyViewModel { public bool IsQuoted { get; set; } } public class AlwaysVisibleConverter : IValueConverter { #region Implementation of IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion } 我修改了Phil的答案以使TargetNullValue起作用: public class AlwaysVisibleConverter : IValueConverter { public object? Convert(object? value, Type targetType, object parameter, CultureInfo culture) { if (value is null) { // Makes TargetNullValue work. // Apparently TargetNullValue doesn't look at the target directly, but through this converter. return null; } return Visibility.Visible; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 并且: <CheckBox Content="Is quoted" IsChecked="{Binding IsQuoted}" Visibility="{Binding IsQuoted, Converter={StaticResource AlwaysVisibleConverter}, FallbackValue=Collapsed TargetNullValue=Collapsed}" />

回答 2 投票 0

WPF 双向绑定会话错误被隐藏

我想将 DataRowView 绑定到窗口上的控件。 初始化代码: ... 行=查询.DefaultView[0]; 数据上下文=行; XAML: ...

回答 1 投票 0

在同一时间轴上对两个对象进行动画处理。相继。 WPF

如何在同一时间线但以不同的时间间隔对两个对象的不透明度进行动画处理? 我期待这两个文本元素逐渐淡入,从一个文本框到另一个文本框。相反,Th...

回答 1 投票 0

将可绑定属性与命令同步

有没有办法在属性以指定的绑定延迟发生变化时执行命令? 作为示例,让我们使用具有 IsChecked 属性且 Delay=1000(1 秒)的 CheckBox 和调用...的 Command

回答 1 投票 0

如何为数据绑定组合框预定义组合框项?

我想允许用户选择串口的波特率。 我创建了一个与串口波特率绑定的文本框,如下所示,它可以工作。 我想允许用户选择串口的波特率。 我创建了一个与串口波特率绑定的文本框,如下所示,它可以工作。 <TextBox x:Name="tbbaudRate" Text="{Binding SerialPort.BaudRate}" /> 我的问题是,有效波特率的设置是有限的。有效波特率为 { 75, 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 }。我想将文本框更改为列出有效波特率值的组合框。 这就是我所做的。 <ComboBox x:Name="tbbaudRate" Text="{Binding SerialPort.BaudRate}" > <ComboBoxItem Content="75"/> <ComboBoxItem Content="110"/> <ComboBoxItem Content="300"/> <ComboBoxItem Content="1200"/> <ComboBoxItem Content="2400"/> <ComboBoxItem Content="4800"/> <ComboBoxItem Content="9600"/> <ComboBoxItem Content="19200"/> <ComboBoxItem Content="38400"/> <ComboBoxItem Content="57600"/> <ComboBoxItem Content="115200"/> </ComboBox> 虽然这有效,但我几乎没有问题。 当我第一次加载窗口时,未选择波特率的默认值(9600)。 这看起来不太优雅。实现这一目标的最佳方法是什么? 供参考,这是我的串口类。也像上面的代码一样丑陋。我使用 resharper 自动生成 notificationpropertychange 代码。 class SerialComm : INotifyPropertyChanged { private int[] ValidBaudRate = new[] { 75, 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 }; //Dont know how to use this private int[] ValidDataBits = new[] { 5, 6, 7, 8, 9 }; //Dont know how to use this private SerialPort _serialPort; public SerialComm() { _serialPort = new SerialPort(); } public SerialPort SerialPort { get { return _serialPort; } set { _serialPort = value; OnPropertyChanged("SerialPort"); SerialPort.GetPortNames(); } } #region Autogenerate by resharper public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion } 像这样改变你的组合框: <ComboBox Name="comboBox1" Width="120" ItemsSource="{Binding Path=ValidBaudRateCollection}"> <ComboBox.ItemTemplate> <DataTemplate> <Label Content="{Binding }"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> 将这些添加到您的SerialComm课程中: public ObservableCollection<int> ValidBaudRateCollection; public SerialComm() { this.ValidBaudRateCollection = new ObservableCollection<int>(this.ValidBaudRate); _serialPort = new SerialPort(); } 最后将它们添加到您的Window中的某个位置(例如构造函数) SerialComm s = new SerialComm(); comboBox1.DataContext = s; comboBox1.ItemsSource = s.ValidBaudRateCollection; comboBox1.SelectedIndex = 6; 注意: 这样您就可以绑定组合框值,但是将 ObservableCollection 添加到似乎位于另一层的类中可能在架构上不正确。 要使“9600”成为默认波特率,您需要添加以下行 myComboBox.SelectedIndex = 7; 9600 排在第七位 希望有帮助... 旧线程,但让我走上了正轨: 通过添加 SelectedValuePath="Content" 并将其保存到 SelectedValue 来解决它。 <ComboBox SelectedValue="{Binding LaserBaudRate, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Content"> <ComboBoxItem Content="75" /> <ComboBoxItem Content="110" /> <ComboBoxItem Content="300" /> <ComboBoxItem Content="1200" /> <ComboBoxItem Content="2400" /> <ComboBoxItem Content="4800" /> <ComboBoxItem Content="9600" /> <ComboBoxItem Content="19200" /> <ComboBoxItem Content="38400" /> <ComboBoxItem Content="57600" /> <ComboBoxItem Content="115200" /> </ComboBox> 只需添加: private int selectedBaudRate; public Constructor() { SelectedBaudRate = Properties.Settings.Default.SelectedBaudRate; } public int SelectedBaudRate { get => selectedBaudRate; set { if (value != selectedBaudRate) { OnPropertyChanging(); selectedBaudRate = value; Properties.Settings.Default.SelectedBaudRate = value; OnPropertyChanged(); } } } <ComboBox Width="80" ItemsSource="{Binding AvailableBaudRate}" SelectedValue="{Binding SelectedBaudRate, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />

回答 4 投票 0

MAUI XAML 中的条件标签颜色

我有一个绑定到列表的 StackLayout,我想将每个项目的 Text 属性放在标签中,并根据 IsSpecial 属性使用不同的颜色,如下所示: 我有一个绑定到列表的 StackLayout,我想将每个项目的 Text 属性放入标签中,并根据 IsSpecial 属性以以下方式使用不同的颜色: <StackLayout BindableLayout.ItemsSource="{Binding Lines}"> <BindableLayout.ItemTemplate> <DataTemplate> <Label Text="{Binding Text}" TextColor= "Red if IsSpecial otherwise White"/> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> 我尝试使用触发器,但我不知道如何正确引用目标类型。 由于您没有显示要绑定的模型类,我将做出有根据的猜测: public class Line { public string Text { get; set; } public bool IsSpecial { get; set; } } 然后你可以像这样使用DataTrigger: <StackLayout BindableLayout.ItemsSource="{Binding Lines}"> <BindableLayout.ItemTemplate> <DataTemplate x:DataType="model:Line"> <Label Text="{Binding Text}" TextColor="White"> <Label.Triggers> <DataTrigger TargetType="Label" Binding="{Binding IsSpecial}" Value="true"> <Setter Property="TextColor" Value="Red" /> </DataTrigger> </Label.Triggers> </Label> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout>

回答 1 投票 0

Uno 平台支持 TreeView 吗?

我有一个在 Uno 平台上运行的 WinUI3 应用程序,我正在尝试实现 TreeView。 我尝试了在互联网上找到的几个示例,但没有一个效果很好。唯一一个拥有...

回答 1 投票 0

您如何知道哪些项目在 ItemsRepeater 中实现(非虚拟化)?

我有一个 ItemsRepeater。我知道我可以使用 ItemsRepeater.ItemsSourceView 获取所有元素,但如何找出哪些元素是实现的(而不是虚拟化的)(例如,用于聚焦右侧...

回答 1 投票 0

WINUI 3.0 - 缺少 WeakEventManager

我在WINUI3中找不到WeakEventManger类。 当我订阅比 ViewModel 寿命更长的模型事件时,我的所有 WPF 项目都在视图模型中使用此属性。 寿命较长的 ViewModel

回答 0 投票 0

.NET MAUI 跨页面共享 ContentView 中的数据

我在 MAUI 上是个菜鸟,我正在为 Android 和 iOS 开发一个应用程序。我有一个简单的 ContentView,它只是一个带有开关的横幅,我将其包含在我的应用程序的几个页面中。我的 xaml 看起来

回答 1 投票 0

C#:无法仅从 MainWindow.xaml.cs 文件访问资源字典

在 Visual Studio 2022 中,我在 C# WPF 应用程序项目根目录的“Resources”文件夹中创建了一个 Resources.resx 文件。在所有课程中,我都可以访问资源库...

回答 1 投票 0

Visual Studio (2022) 是否有“XAML 设计调试输出”窗口?

我有两个xaml文件: 我可以在设计视图中看到一个(存在于解决方案1中)。 另一个(存在于解决方案2中)我在设计视图中看不到。 第二个xaml没有任何问题:我...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.