WPF 绑定标签内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43001829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF binding label content
提问by Alex Wyler
Have a trouble with binding to label content
绑定标签内容有问题
I have special custom TabControl in page. Bind SelectedTab property from page viewmodel to controlview model to get actualSelectedTab
我在页面中有特殊的自定义 TabControl。将页面视图模型中的 SelectedTab 属性绑定到控制视图模型以获取实际的SelectedTab
public int SelectedTab
{
get { return _selectedTab; }
set
{
SetProperty(ref _selectedTab, value);
}
}
For example, my tab control has 3 tabs; When tab one is selected - Selected Tab value is 0, etc.
例如,我的选项卡控件有 3 个选项卡;选择选项卡一时 - 选定的选项卡值为 0,依此类推。
But I need to show what current tab is selected in mainPage like 1/3 - 2/3 - 3/3
但我需要像 1/3 - 2/3 - 3/3 一样显示在 mainPage 中选择的当前选项卡
My final result must be like:
我的最终结果必须是这样的:
Selected Tab 1/3 ... 3/3
所选标签 1/3 ... 3/3
<Label
Margin="5 0 28 0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
TextElement.FontSize="12"
TextElement.FontWeight="Bold"
TextElement.Foreground="White"
VerticalContentAlignment="Center"
Content="{Binding SelectedTab, Mode=OneWay}">
</Label>
回答by Mighty Badaboom
The problem is that you are not updating the UI in your property. You have to implement INotifyPropertyChanged in your ViewModel like this
问题是您没有更新您的财产中的 UI。你必须像这样在你的 ViewModel 中实现 INotifyPropertyChanged
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public int SelectedTab
{
get { return _selectedTab; }
set
{
SetProperty(ref _selectedTab, value);
OnPropertyChanged("SelectedTab");
}
}
}
You're Labelshould show now the SelectedTab(0, 1, 2, etc.). When you want do display e.g. 1/3 you should do this with an IValueConverter.
您现在Label应该显示SelectedTab(0, 1, 2, etc.)。当您想要显示例如 1/3 时,您应该使用IValueConverter.
You need to implement IValueConverter
你需要实施 IValueConverter
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
var tabIndex = int.Parse(value.ToString());
return tabIndex + 1;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
//don't needed
}
}
And in your xaml change your binding like
Content="{Binding SelectedTab, Converter={StaticResource MyConverter}, Mode=OneWay}
并在您的 xaml 中更改您的绑定,例如
Content="{Binding SelectedTab, Converter={StaticResource MyConverter}, Mode=OneWay}
And in Windowor UserControladd this in your resources to access the converter
并在您的资源中添加Window或UserControl添加它以访问转换器
<Window.Resources>
<local:MyConverter x:Key="MyConverter"/>
</Window.Resources>

