gpt4 book ai didi

c# - Windows 应用商店应用程序栏按钮文本绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 22:21:38 26 4
gpt4 key购买 nike

我有一个应用栏,上面有一些像这样的按钮

<Page.BottomAppBar>
<AppBar x:Name="bottomAppBar" Padding="10,10,10,10" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Style="{StaticResource ReadAppBarButtonStyle}" >
</Button>
</StackPanel>
</AppBar>
</Page.BottomAppBar>

我想将按钮文本绑定(bind)到 ListView 的选定项属性并使用 IValueConverter

我发现按钮文本要使用AutomationProperties.Name来设置

如何通过XAML代码 绑定(bind)此属性。

谢谢

最佳答案

你是对的,出于某种原因,以下内容不起作用,尽管相同的绑定(bind)工作得很好,你可以将它用于例如TextBoxText 属性:

<Button Style="{StaticResource SkipBackAppBarButtonStyle}" AutomationProperties.Name="{Binding SelectedItem, ElementName=List}" />

我确实设法通过在 View 模型中使用一个属性并将其绑定(bind)到 ListView.SelectedItemAutomationProperties.Name 来让它工作:

<ListView ItemsSource="{Binding Strings}" 
SelectedItem="{Binding SelectedString, Mode=TwoWay}" />
<!-- ... -->
<Button Style="{StaticResource SkipBackAppBarButtonStyle}"
AutomationProperties.Name="{Binding SelectedString}" />

SelectedString 应该是实现 INotifyPropertyChanged 的 View 模型中的一个属性:

public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Strings = new ObservableCollection<string>();
for (int i = 0; i < 50; i++)
{
Strings.Add("Value " + i);
}
}

public ObservableCollection<string> Strings { get; set; }

private string _selectedString;
public string SelectedString
{
get { return _selectedString; }
set
{
if (value == _selectedString) return;
_selectedString = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}

关于c# - Windows 应用商店应用程序栏按钮文本绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14215358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com