首页 > 其他分享 >WPF string format

WPF string format

时间:2024-10-06 19:21:54浏览次数:8  
标签:cnt string format Windows imgsList System using WPF public

Text="{Binding StringFormat={}{0} items,Source={StaticResource mainVM},Path=Cnt}"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp17.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"
        WindowState="Maximized"
        xmlns:local="clr-namespace:WpfApp17"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:MainVM x:Key="mainVM"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" 
                 ItemsSource="{StaticResource mainVM}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding DataContext,RelativeSource={RelativeSource 
                        Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}}"
                        Width="500"
                        Height="800"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock Grid.Row="1" 
                   Text="{Binding StringFormat={}{0} items,Source={StaticResource mainVM},Path=Cnt}"
                   Panel.ZIndex="20" 
                   FontSize="100" 
                   Foreground="Red"/>
    </Grid>
</Window>



//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp17
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
        }
    }

    public class MainVM:ObservableCollection<string>,INotifyPropertyChanged
    {
        private int cnt;
        public int Cnt
        {
            get
            {
                return cnt;
            }
            set
            {
                if(value!=cnt)
                {
                    cnt = value;
                    
                }
            }
        }
        
        public MainVM()
        {
            var imgsList=new List<string>(Directory.GetFiles(@"../../Images"));
            if(imgsList!=null && imgsList.Any())
            {
                Cnt= imgsList.Count;
                foreach(var im in imgsList.Take(20))
                {
                    this.Add(im);
                }
            }
        }
    }
}

 

标签:cnt,string,format,Windows,imgsList,System,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18449320

相关文章

  • WPF ListBoxItem Selected and background changed at the same time via ItemContain
    <Window.Resources><Stylex:Key="lbxItemContainerStyle"TargetType="ListBoxItem"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType=&quo......
  • WPF ListBox IsSynchronizedWithCurrentItem True ScrollIntoView via behavior CallM
    <ListBoxGrid.Column="0"ItemContainerStyle="{StaticResourcelbxItemContainerStyle}"ItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"IsSynchronizedWith......
  • WPF ListBox IsSynchronizedWithCurrentItem="True"
    //xaml<Windowx:Class="WpfApp13.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • WPF VirtualizingPanel.CacheLength="1" VirtualizingPanel.CacheLengthUnit="Item" c
    <ListBoxItemsSource="{BindingBooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectedIndex="{BindingImgIdx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"VirtualizingPanel.IsVirtualizing=......
  • 本地环境PHP帝国备份王备份报错mysql_escape_string(): This function is
    在使用帝国备份王进行备份和恢复时,如果遇到PHP5.5环境下的报错,通常是因为一些旧的MySQL函数已经被弃用或移除。具体来说,mysql_escape_string 函数在PHP5.5中已经被废弃,应该使用 mysql_real_escape_string 替代。解决方案定位问题文件:找到 function.php 文件的位置。......
  • 手打C库头文件<string.h>
    我是标题党,其实是只实现了部分函数本人实力不济,不会讲解(绝对不是我懒)只能直接贴上代码:/*未经测试,准确率无法保证(如有错误,欢迎指出)其中strcoll(),strerror(),strxfrm()无法实现*/#if!definedMy_cstring&&!defined_INC_STRING#defineMy_cstring#define......
  • 【C++】 string类的模拟实现
    目录string类各函数接口总览构造函数拷贝构造函数赋值运行符重载函数析构函数迭代器相关函数beginend容量和大小相关的函数sizecapacityresizereserveempty修改字符串相关函数push_backappendoperator+=inserteraseclearswapc_str访问字符串相关函数o......
  • String
    String题面:给定两个字符串\(a\),\(b\),我们称这两个字符串的所有子序列为坏字符串。求最短的非坏字符串。做法:首先要解决一个问题,假设你有一个字符串你需要判断这个字符串是否是坏的,怎么快速判断?我们预处理出nxta[i][j]表示\(a\)字符串中第\(i\)个位置之后第一个\(......
  • WPF 闪烁动画
    实现使用一个Boder控件,通过后台属性控制Boder的背景色变化,实现闪烁的效果。1.xaml视图代码<BorderWidth="15"Height="15"Margin="25000"><Border.Style><StyleTargetType="{x:TypeBorder}"><SetterPro......
  • WPF ListBox ListBoxItemTemplate display image automatically via System.Timers.Ti
    //xaml<Windowx:Class="WpfApp6.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......