uwp-xaml 相关问题

此标记用于Windows 10上Windows-Store-Apps特有的XAML UI框架,是通用Windows平台(UWP)的一部分。

弹出窗口打开时滚动内容

带有两个控件的简单页面,Button 和 GridView。按钮打开带有其他几个按钮的弹出窗口。 Gridview有一个大约100个元素的列表,所以有滚动。如果按钮的弹出按钮是...

回答 1 投票 0

在 XAML 中向 MenuFlyoutItem 添加图标

我正在尝试向 MenuFlyoutItem 添加一个图标。我有以下项目结构: 项目 -资源 - 图片 ---图片.png - 景观 --视图.xaml XAML如下: 我正在尝试向 MenuFlyoutItem 添加一个图标。我有以下项目结构: 项目 -资源 --图片 ---图片.png -观点 --查看.xaml XAML如下: <MenuFlyoutItem Command="{x:Bind ViewModel.SomeCommand}" Style="{StaticResource SomeStyle}" Width="Auto"> <MenuFlyoutItem.Icon> <BitmapIcon UriSource="/Resources/Images/image.png"/> </MenuFlyoutItem.Icon> </MenuFlyoutItem> 但我在菜单项中没有看到该图标。 我期待有这样的事情: 我尝试将图像控件添加到视图中并指定“/Resources/Images/image.png”作为图像源,只是为了检查是否可以访问该 image.png 文件,在本例中是我的图像显示没有问题。 请帮助我,我对 MenuFlyoutItem 做错了什么? 谢谢。 1.请检查文件夹中是否有image.png /Resources/Images/ 2.请在解决方案资源管理器中右键单击 image.png,选择属性。 在“属性”窗口中,检查 Build Action 是否为内容。

回答 1 投票 0

关闭文本框的拼写检查

我想创建一个当用户使用软件键盘时不进行拼写检查的文本框。我已经尝试过这个: 我想创建一个当用户使用软件键盘时不进行拼写检查的文本框。我试过这个: <TextBox HorizontalAlignment="Stretch" IsSpellCheckEnabled="False" IsTextPredictionEnabled="False" /> 但不知何故,拼写检查仍然出现在软件键盘中。有什么建议吗? 您可以在 WinUI 中使用 IsSpellCheckEnabled 属性: <TextBox PlaceholderText="Benutzername" x:Name="tbUser" IsSpellCheckEnabled="False" />

回答 1 投票 0

UWP:在按钮单击事件上取消选中 ListView 内的复选框

我正在尝试清除弹出窗口=>按钮单击事件上的ListView中选定的复选框。 我尝试过循环 listview 并将属性设置为 false 但它不起作用。 我正在尝试清除弹出窗口中选定的复选框=>按钮单击事件上的ListView。 我尝试过循环列表视图并将属性设置为 false,但它不起作用。 <Page x:Class="UC.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UC" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:mvsc="using:UC.Models" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <StackPanel> <Button x:Name="btn_Add" Content="Add" Margin="420,10,10,0" Click="Btn_Add_Click"/> </StackPanel> <StackPanel Margin="10,50,10,0"> <TextBlock Text="Selected List" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" /> <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" BorderThickness="1" Margin="10,0,800,5" Height="300"> <ListView x:Name="lstViewSelectedData" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Enabled"> <ListView.ItemTemplate> <DataTemplate x:DataType="mvsc:SelectedDataModel"> <StackPanel Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" x:Name="txtSelectedData" Text="{x:Bind Message}"></TextBlock> <AppBarButton VerticalAlignment="Bottom" Click="AppBarButton_Click" Icon="Delete" Tag="{x:Bind Id, Mode=OneWay}" > </AppBarButton> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Border> </StackPanel> <Popup x:Name="popup1" VerticalOffset="10" HorizontalOffset="450" IsLightDismissEnabled="True" HorizontalAlignment="Stretch" Margin="50" VerticalAlignment="Stretch"> <Border BorderBrush="{StaticResource ApplicationForegroundThemeBrush}" Background="{StaticResource ApplicationPageBackgroundThemeBrush}" BorderThickness="1" Width="450" Height="400" > <StackPanel x:Name="stkpnl1" Orientation="Vertical"> <TextBox x:Name="txtSearch" Header="Filter" Margin="10" PlaceholderText="Search list" TextChanging="TxtSearch_TextChanging" /> <ListView x:Name="lstView" Margin="30" Height="200" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Enabled"> <ListView.ItemTemplate > <DataTemplate x:DataType="mvsc:SelectedDataModel" > <CheckBox x:Name="chkBox" FlowDirection="LeftToRight" Tag="{x:Bind Id, Mode=TwoWay}" Content="{x:Bind Message, Mode=TwoWay}" IsChecked="{x:Bind IsFavorite,Mode=TwoWay}" Checked="ChkBox_Checked" Unchecked="ChkBox_Unchecked" IsHitTestVisible="True"/> </DataTemplate> </ListView.ItemTemplate> </ListView> <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" FlowDirection="LeftToRight" > <Button x:Name="btnAdd" Content="Select" Click="BtnAdd_Click" Margin="10" /> <Button x:Name="btnCancel" Content="Close" Click="BtnCancel_Click"/> </StackPanel> </StackPanel> </Border> </Popup> </Grid> </Page> public sealed partial class MainPage : Page { public SelectedDataModel SelectedData { get { return (SelectedDataModel)GetValue(SelectedDataProperty); } set { SetValue(SelectedDataProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedDataProperty = DependencyProperty.Register("SelectedData", typeof(int), typeof(SelectedDataModel), null); public AllDataModel AllDataList { get { return (AllDataModel)GetValue(AllDataListProperty); } set { SetValue(AllDataListProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty AllDataListProperty = DependencyProperty.Register("AllDataList", typeof(int), typeof(AllDataModel), null); List<SelectedDataModel> listData = new List<SelectedDataModel>(); List<SelectedDataModel> temp = new List<SelectedDataModel>(); List<SelectedDataModel> temp1 = new List<SelectedDataModel>(); List<SelectedDataModel> selectedData = new List<SelectedDataModel>(); public MainPage() { this.InitializeComponent(); lstView.ItemsSource = GetData(); //lstViewSelectedData.ItemsSource = GetSelectedData(); temp = listData; } public List<SelectedDataModel> GetData() { listData.Add(new SelectedDataModel { Id = 1, Message = "One" }); listData.Add(new SelectedDataModel { Id = 2, Message = "Two" }); listData.Add(new SelectedDataModel { Id = 3, Message = "Three" }); listData.Add(new SelectedDataModel { Id = 4, Message = "Four" }); listData.Add(new SelectedDataModel { Id = 5, Message = "Five" }); listData.Add(new SelectedDataModel { Id = 6, Message = "Six" }); listData.Add(new SelectedDataModel { Id = 7, Message = "Seven" }); listData.Add(new SelectedDataModel { Id = 8, Message = "Eight" }); return listData; } private async void BtnAdd_Click(object sender, RoutedEventArgs e) { if (selectedData.Count == 0) { ContentDialog dialog = new ContentDialog(); dialog.Title = "Not Selected"; dialog.Content = "Please Select Atlease One Entry"; dialog.CloseButtonText = "Close"; await dialog.ShowAsync(); } else { lstViewSelectedData.ItemsSource = null; var dt = selectedData; lstViewSelectedData.ItemsSource = dt; //this.selectedData.ForEach(x => x.IsFavorite = false); //this.listData.ForEach(x => x.IsFavorite = false); //temp1 = dt; popup1.IsOpen = false; //foreach (SelectedDataModel row in lstView.Items.ToList()) //{ // //CheckBox box = row.FindName("chkSelect") as CheckBox; // if (row.IsFavorite == true) // { // row.IsFavorite = false; // // listData.Remove(listData.First(X => X.Id == row.Id)); // //Logic to delete the row // } //} for (int i = 0; i < lstView.Items.Count; i++) { ListViewItem lvi = (ListViewItem)lstView.ItemContainerGenerator.ContainerFromItem(lstView.Items[i]); if (lvi != null) { //CheckBox c = lvi.FindChildByType<CheckBox>(); //c.IsChecked = true; } } } } private void TxtSearch_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { listData = temp; if (!string.IsNullOrEmpty(txtSearch.Text)) { listData = listData.Where(x => x.Message.ToUpper().Contains(txtSearch.Text.ToUpper())).ToList(); if (listData.Count > 0) { lstView.ItemsSource = listData; } else { lstView.ItemsSource = temp; } } else { lstView.ItemsSource = temp; } } private void ChkBox_Checked(object sender, RoutedEventArgs e) { var chkBox = sender as CheckBox; //throwing an exception System.NullReferenceException: 'Object reference not set to an instance of an object.' selectedData.Add(listData.First(x => x.Id == int.Parse(chkBox.Tag.ToString()))); // listData.Remove(listData.First(X => X.Id == int.Parse(chkBox.Tag.ToString()))); //selectedData.Clear(); } private void ChkBox_Unchecked(object sender, RoutedEventArgs e) { var chkBox = sender as CheckBox; selectedData.Remove(listData.First(X => X.Id == int.Parse(chkBox.Tag.ToString()))); } private void BtnAddData_Click(object sender, RoutedEventArgs e) { //popup2.IsOpen = false; popup1.IsOpen = true; } private void BtnCancel_Click(object sender, RoutedEventArgs e) { popup1.IsOpen = false; } private void Btn_Add_Click(object sender, RoutedEventArgs e) { popup1.IsOpen = true; } private async void AppBarButton_Click(object sender, RoutedEventArgs e) { AppBarButton delButton = sender as AppBarButton; ContentDialog dialog = new ContentDialog(); dialog.Content = "Do You Want To Remove..?"; dialog.PrimaryButtonText = "Yes"; dialog.SecondaryButtonText = "No"; dialog.Title = "Remove"; ContentDialogResult result = await dialog.ShowAsync(); if (result == ContentDialogResult.Primary) { //var isRemoved = temp.Remove(listData.First(x => x.id == int.Parse(delButton.Tag.ToString()))); //List<Info> temp1 = new List<Info>(); //foreach (var item in temp) //{ // temp1.Add(item); //} //lstViewSelectedData.ItemsSource = temp1; var temp2 = selectedData; temp2.Remove(temp1.First(x => x.Id == int.Parse(delButton.Tag.ToString()))); lstViewSelectedData.ItemsSource = null; lstViewSelectedData.ItemsSource = temp2; } } } 我只想清除按钮单击事件 UWP 上列表视图内的选中项目。 我尝试过循环列表视图并将属性设置为 false,但它不起作用。 您的复选框绑定到 IsFavorite 属性。因此,如果您想取消选中它,只需更新 IsFavorite 属性的值即可。要在属性值更改时更新 UI,您的 SelectedDataModel 类需要实现 INotifyPropertyChanged Interface。 public class SelectedDataModel : INotifyPropertyChanged { private int _Id; public int Id { get { return _Id; } set { if (_Id != value) { _Id = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Id")); } } } private string _Message; public string Message { get { return _Message; } set { if (_Message != value) { _Message = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Message")); } } } private bool _IsFavorite; public bool IsFavorite { get { return _IsFavorite; } set { if (_IsFavorite != value) { _IsFavorite = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsFavorite")); } } } public event PropertyChangedEventHandler PropertyChanged; } 然后,在按钮单击事件处理程序中,您只需将listData中每个项目的 IsFavorite 更改为 false。 foreach (var item in listData) { item.IsFavorite = false; }

回答 1 投票 0

将 ToggleSwitch 设置为开或关

我有 3 个切换开关,分别名为 onlineToggle、offlineToggle 和 maxToggle。 XAML: 我有 3 个切换开关,分别名为 onlineToggle、offlineToggle 和 maxToggle。 XAML: <ToggleSwitch x:Name="onlineToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" PointerReleased="onlineToggleSwitch_PointerReleased"> </ToggleSwitch> <ToggleSwitch x:Name="offlineToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" PointerReleased="offlineToggleSwitch_PointerReleased"> </ToggleSwitch> <ToggleSwitch x:Name="maxToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" PointerReleased="maxToggleSwitch_PointerReleased"> </ToggleSwitch> 我想要如果 onlineToggle isOn = true,则offlineToggle 和 maxToggle 关闭。如果offlineToggle isOn = true,则onlineToggle 和maxToggle 关闭。如果 maxToggle isOn = true,则 onlineToggle 和 OfflineToggle 关闭。 代码: private void onlineToggleSwitch_PointerReleased(object sender, PointerRoutedEventArgs e) { onlineToggleSwitch.IsOn = true; offlineToggleSwitch.IsOn = false; maxToggleSwitch.IsOn = false; } private async void offlineToggleSwitch_PointerReleased(object sender, PointerRoutedEventArgs e) { onlineToggleSwitch.IsOn = false; offlineToggleSwitch.IsOn = true; maxToggleSwitch.IsOn = false; } private void maxToggleSwitch_PointerReleased(object sender, PointerRoutedEventArgs e) { onlineToggleSwitch.IsOn = false; offlineToggleSwitch.IsOn = false; maxToggleSwitch.IsOn = true; } 但是我有一个问题,即如果 onlineToggle isOn = true,那么我想更改 maxToggle = true,那么 Toogleswitches 都关闭(maxToggle 不会打开),如下视频所示: https://1drv.ms/v/s!Av6G8Zq_Px8Whi9eSZTSDPqMUCdA?e=25cJtD 如何处理? 根据您的描述,您应该最多选择三个切换开关之一。 建议使用 Toggled 事件而不是 PointerReleased 事件。判断事件中当前ToggleSwitch是否为IsOn,然后关闭另外两个ToggleSwitch。 <ToggleSwitch x:Name="onlineToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" Toggled="onlineToggleSwitch_Toggled"> </ToggleSwitch> <ToggleSwitch x:Name="offlineToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" Toggled="offlineToggleSwitch_Toggled"> </ToggleSwitch> <ToggleSwitch x:Name="maxToggleSwitch" VerticalAlignment="Center" FlowDirection="LeftToRight" OffContent="" OnContent="" Toggled="maxToggleSwitch_Toggled"> </ToggleSwitch> private void onlineToggleSwitch_Toggled(object sender, RoutedEventArgs e) { if (onlineToggleSwitch.IsOn == true) { offlineToggleSwitch.IsOn = false; maxToggleSwitch.IsOn = false; } } private void offlineToggleSwitch_Toggled(object sender, RoutedEventArgs e) { if (offlineToggleSwitch.IsOn == true) { onlineToggleSwitch.IsOn = false; maxToggleSwitch.IsOn = false; } } private void maxToggleSwitch_Toggled(object sender, RoutedEventArgs e) { if (maxToggleSwitch.IsOn == true) { onlineToggleSwitch.IsOn = false; offlineToggleSwitch.IsOn = false; } }

回答 1 投票 0

如何确定 UWP 中框架显示的当前页面

我对 UWP 开发相当陌生,到目前为止,我按照下面的教程制作了一个程序,该程序使用 NavigationView 进行选择并使用 Frame 加载页面来浏览页面。但我...

回答 1 投票 0

仅使用 DataTable WinUI3 MVVM 中的数据填充 DataGrid

数据表填充有数据库中的数据。 var query = $"SELECT * FROM {databaseTable.Name}"; OracleCommand oracleCommand = new OracleCommand(query, _connection);

回答 1 投票 0

使用 c# 修改 SVG 文件的属性并将其设置为 uwp 中图像元素的源

string svgFilePath = "ms-appx:///Assets/Chart.svg"; XNamespace svgNamespace = "http://www.w3.org/2000/svg"; StorageFile svgFile = 等待 StorageFile.GetFileFromApplicationUriAsy...

回答 1 投票 0

我正在尝试使用 CotentDialog 获取一些数据并在 UWP XAML 的数据库中更改它,但遇到了一些问题

private async void iconu_PointerPressed(对象发送者,PointerRoulatedEventArgs eventArgs) { if (BigGrid.SelectedIndex == -1) { 内容对话框dialog2 = 新的内容对话框 { ...

回答 1 投票 0

有条件地拖放/可重新排列TreeView

我在 UWP 应用程序中设置拖放树视图时遇到问题,用户可以对树中的项目进行重新排序。有一个演示应用程序,我用它来满足我的需要......

回答 1 投票 0

UWP:显示完整尺寸的图像

我在 UWP 应用程序中遇到问题,我试图在 ContentDialog 中显示图像。但是,图像在对话框中被裁剪,我希望它在没有任何裁剪的情况下显示......

回答 1 投票 0

我可以使用 x:Bind 从 DataTemplate 中绑定到视图模型吗?

最低限度的示例: 最基本的例子: <DataTemplate x:Key="CustomTemplate" x:DataType="data:CustomType"> <TextBlock Text="{x:Bind Foo}" Foreground="{x:Bind viewModel.GetColor(Foo), Mode=OneWay}" /> <TextBlock Text="{x:Bind Bar}" /> </DataTemplate> 在此代码中,无法找到 viewModel,因为数据模板正在寻找 viewModel 内的 CustomType。这使用了 x:Bind 的功能,该功能允许在绑定中使用函数。这意味着我无法将 RelativeSource 与 Binding 一起使用来设置绑定查找 viewModel 的路径(尽管即使没有该函数我也无法让它工作)。 有一些非常简单的解决方法。例如,由于 CustomType 是一个 ObservableObject,因此创建一个绑定可以直接读取的 Color 属性是没有问题的,但是因为 CustomType 只是一种数据类型,所以它不应该关心其中发生的情况UI 方面的事情。或者,包装器类(CustomTypeUi或类似的)可以公开额外的功能,但是仅仅针对这样的东西的包装器看起来应该非常微不足道,感觉是错误的,如果可能的话我想避免它。 有解决这个问题的惯用方法吗? 如果DataTemplate是Page的资源,您可以x:Name您的Page,例如ThisPage,然后: <DataTemplate> <TextBlock Text="{Binding ElementName=ThisPage, Path=ViewModel.AwesomeString, Mode=OneWay}" /> </DataTemplate> 但是: 适用于 Binding,但不适用于 x:Bind,因此无法调用方法。 适用于 ListView 或 GridView,但不适用于 ItemsRepeater。 现在,让我向您展示一个使用值转换器的示例。此示例依赖于 Foo 是 string 或可以使用 ToString(),但您也许可以将其应用到您的案例中。 public class StringToColorDictionary : Dictionary<string, Color> { } public class ObjectToColorConverter : IValueConverter { public StringToColorDictionary Colors { get; set; } = new(); public object Convert(object value, Type targetType, object parameter, string language) { return value.ToString() is string stringValue && Colors.TryGetValue(stringValue, out var color) ? new SolidColorBrush(color) : new SolidColorBrush(Microsoft.UI.Colors.Transparent); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } <Page.Resources> <local:ObjectToColorConverter x:Key="ObjectToColorConverter"> <local:ObjectToColorConverter.Colors> <local:StringToColorDictionary> <Color x:Key="A">Red</Color> <Color x:Key="B">Green</Color> <Color x:Key="C">Blue</Color> </local:StringToColorDictionary> </local:ObjectToColorConverter.Colors> </local:ObjectToColorConverter> <DataTemplate x:Key="ItemTemplate" x:DataType="local:Item"> <TextBlock Foreground="{x:Bind Foo, Converter={StaticResource ObjectToColorConverter}}" Text="{x:Bind Foo}" /> </DataTemplate> </Page.Resources> <ListView ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}" /> 另一种方法是将这个 FrameworkElement.DataContext 绑定到 view 元素。请参阅@mm8的回答https://stackoverflow.com/a/70703773。但似乎view不能是Microsoft.UI.Xaml.Window。 我的测试: <ResourceDictionary> <DataTemplate x:Key="MyDataTemplate" x:DataType="local:Customer"> <StackPanel Orientation="Horizontal"> <TextBlock local:AncestorSource.AncestorType="ListBox" Text="{Binding Tag}" /> ... </StackPanel> </DataTemplate> </ResourceDictionary> 和 <Window x:Class="App2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App2" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <ListBox ItemsSource="{x:Bind Customers}" Width="350" Margin="0,5,0,10" ItemTemplate="{StaticResource MyDataTemplate}" Tag="aTag"/> </StackPanel> </Window>

回答 2 投票 0

我可以从 DataTemplate 中绑定到视图模型吗?

最低限度的示例: 最基本的例子: <DataTemplate x:Key="CustomTemplate" x:DataType="data:CustomType"> <TextBlock Text="{x:Bind Foo}" Foreground="{x:Bind viewModel.GetColor(Foo), Mode=OneWay}" /> <TextBlock Text="{x:Bind Bar}" /> </DataTemplate> 在此代码中,无法找到 viewModel,因为数据模板正在寻找 viewModel 内的 CustomType。这使用了 x:Bind 的功能,该功能允许在绑定中使用函数。这意味着我无法将 RelativeSource 与 Binding 一起使用来设置绑定查找 viewModel 的路径(尽管即使没有该函数我也无法让它工作)。 有一些非常简单的解决方法。例如,由于 CustomType 是一个 ObservableObject,因此创建一个绑定可以直接读取的 Color 属性是没有问题的,但是因为 CustomType 只是一种数据类型,所以它不应该关心其中发生的情况UI 方面的事情。或者,包装器类(CustomTypeUi或类似的)可以公开额外的功能,但是仅仅针对这样的东西的包装器看起来应该非常微不足道,感觉是错误的,如果可能的话我想避免它。 有解决这个问题的惯用方法吗? 让我向您展示一个使用值转换器的示例。在此示例中,依赖于 Foo 是 string 或可以使用 ToString(),但您也许可以将其应用到您的案例中。 public class StringToColorDictionary : Dictionary<string, Color> { } public class ObjectToColorConverter : IValueConverter { public StringToColorDictionary Colors { get; set; } = new(); public object Convert(object value, Type targetType, object parameter, string language) { return value.ToString() is string stringValue && Colors.TryGetValue(stringValue, out var color) ? new SolidColorBrush(color) : new SolidColorBrush(Microsoft.UI.Colors.Transparent); } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); } } <Page.Resources> <local:ObjectToColorConverter x:Key="ObjectToColorConverter"> <local:ObjectToColorConverter.Colors> <local:StringToColorDictionary> <Color x:Key="A">Red</Color> <Color x:Key="B">Green</Color> <Color x:Key="C">Blue</Color> </local:StringToColorDictionary> </local:ObjectToColorConverter.Colors> </local:ObjectToColorConverter> <DataTemplate x:Key="ItemTemplate" x:DataType="local:Item"> <TextBlock Foreground="{x:Bind Foo, Converter={StaticResource ObjectToColorConverter}}" Text="{x:Bind Foo}" /> </DataTemplate> </Page.Resources> <ListView ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{x:Bind ViewModel.Items, Mode=OneWay}" />

回答 1 投票 0

WinUI XAML:使用另一个项目中的 ResourceDictionary

在我的 WinUI 3 应用程序中,我尝试使用位于另一个项目中的 ResourceDictionary。 假设引用的项目是 ResourceTestLib,并且该库项目有一个文件夹“

回答 1 投票 0

UWP:需要在文本框单击时禁用屏幕键盘

UWP:需要在文本框单击时禁用屏幕键盘 因为我需要调用第三方屏幕键盘。 尝试了下面的代码......但我无法禁用屏幕键盘。

回答 1 投票 0

使用 UWP 中 RichEditBox 的 uri 将字体设置为 DefaultCharacterFormat 会引发异常

公共主页() { this.InitializeComponent(); RichEditBox rbox = new RichEditBox(); Canva.Children.Add(rbox); rbox.Document.SetText(0, "asdfasfasdfasdfasdfoiuytredfxbnuytrxgd...

回答 1 投票 0

UWP - 当指针位于 ListViewItem 上时 ListView 显示按钮

有一个 ListView,其项目包含可见性设置为 Collapsed 的 Button。 当指针悬停在包含该按钮的 ListViewItem 上时,如何使按钮可见? 可以用

回答 1 投票 0

ComboBox SelectedItem 未使用 x:Bind 设置

我试图弄清楚为什么如果我使用 ItemsSource="{x:Bind [source]}" 进行绑定,则无法在 ComboBox 上设置初始 SelectedItem 值。 这个xaml有效 我试图弄清楚为什么如果我与 SelectedItem 绑定,我无法在组合框中设置初始 ItemsSource="{x:Bind [source]}" 值。 这个 xaml 有效 <ComboBox ItemsSource="{Binding Sites, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.SelectedContractSite, Mode=TwoWay}"/> 但是当我更改为以下 xaml 时,ComboBox 包含站点,但默认不显示 SelectedItem。 (事实上,它似乎闪烁进入视野,然后消失)。 <ComboBox ItemsSource="{x:Bind ViewModel.Sites, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.SelectedContractSite, Mode=TwoWay}"/> 这是ViewModel中的相关代码。 (我缩写了很长的站点列表。) public List<string> Sites { get { return new List<string>() { "Miami", "Texas" }; } } private string _selectedContractSite = "Texas"; public string SelectedContractSite { get { return _selectedContractSite; } set { Set(ref _selectedContractSite, value); } } 该问题似乎与您未显示的代码有关。 如果我创建一个像这样的viewModel public class ViewModel : INotifyPropertyChanged { public List<string> Sites { get { return new List<string>() { "Miami", "Texas" }; } } private string _selectedContractSite = "Texas"; public string SelectedContractSite { get { return _selectedContractSite; } set { if (_selectedContractSite != value) { _selectedContractSite = value; OnPropertyChanged(nameof(SelectedContractSite)); } } } public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 然后像这样设置隐藏代码: public MainPage() { this.InitializeComponent(); this.ViewModel = new ViewModel(); } public ViewModel ViewModel { get; set; } 然后以下 XAML 按预期工作 <ComboBox ItemsSource="{x:Bind ViewModel.Sites, Mode=OneWay}" SelectedItem="{x:Bind ViewModel.SelectedContractSite, Mode=TwoWay}" /> 注意。我正在使用 x:Bind 并在两个绑定路径中引用 ViewModel。 我怀疑您的困惑在于 x:Bind 和 Binding 之间的差异。 对于 x:Bind,绑定路径的根是具有绑定的控件所在的页面。 对于 Binding,绑定路径的根是控件所在页面的 DataContext。 混合两者可能会造成混乱。如果您确实需要使用两者的组合,请在页面构造函数中设置 this.DataContext = this;,以便它们都指向同一对象。 为什么要在 List<string> 属性的 getter 中创建一个新的 Sites ? 尝试仅创建源集合一次: public List<string> Sites { get; } = new List<string>() { "Miami", "Texas" };

回答 2 投票 0

WinUI 3 数据绑定适用于一个对象,但不适用于另一个对象

我在 WinUI 数据绑定和 INotifyPropertyChanged 实现方面遇到问题。 我有一个按预期工作的测试类 公共类 BasicNotifyChange : INotifyPropertyChan...

回答 1 投票 0

(UWP) 如何从 DataTemplate 中访问我的代码隐藏文件?

这基本上是一个数据绑定问题。 简而言之,我有一个生成项目的 ListView,并使用 DataTemplate 来呈现这些项目。然而,在 DataTemplate 中,DataContext 会...

回答 1 投票 0

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